LogoLogo
  • Technical Direction
  • Technical overview
    • Technical Implementation
    • API Design Guide
      • Data Definitions and Standards
      • Data Transfer Objects
      • Documentation
      • Environments
      • Error Handling
      • Example API Service
      • GraphQL Naming Conventions
      • Methods
      • Naming Conventions
      • Once Only Principle
      • Pagination
      • Resource Oriented Design
      • REST Request
      • REST Response
      • Security
      • Versioning
    • Ísland.is Public Web Data Flow
    • Code Reviews
    • Code Standards
    • Monorepo
    • Project Management
    • Teamwork
    • Architectural Decision Records
      • Use Markdown Architectural Decision Records
      • Use NX
      • Continuous Integration
      • CSS
      • Branching and Release Strategy
      • Error Tracking and Monitoring
      • What API Management Tool to Consider
      • Viskuausan Static Site Generator
      • Use OAuth 2.0 and OpenID Connect As Protocols for Authentication and Authorization
      • Unified Naming Strategy for Files and Directories
      • CMS
      • Open Source License
      • What Chart Library Should We Use Across Island.is?
      • What Feature Flag Service/application Should We Use at Island.is?
      • Logging, Monitoring and APM Platform
      • ADR Template
    • Log Management Policy
  • Products
    • Island.is Authentication Service
      • Terminology
      • Integration Options
      • Authentication Flows
      • Authorising API Endpoints
      • Session Lifecycle
      • Scopes and Tokens
      • Delegations
      • Configuration
      • Tools and Examples
      • Environments
      • Test IAS with Postman
      • Using the IAS admin portal
    • Notifications / Hnipp
      • New Notification Setup Guide
      • Notifications service workflow overview
      • Email notifications
    • Pósthólfið
      • Security Checklist
      • Introduction
      • Skjalatilkynning API
      • Skjalaveita API
      • Sequence Diagram
      • Interfaces
    • Straumurinn (X-Road)
      • Architecture Guidelines for Service Providers and Consumers
      • Setting up an X-Road Security Server
        • Network Configuration
      • X-Road - Uppfærsla á öryggisþjónum
      • Straumurinn - Notkun og umsýsla
      • X-Road Central - current version
  • Development
    • Getting Started
    • Generating a New Project
    • Definition of done
    • Devops
      • Continuous Delivery
      • Database
      • Dockerizing
      • Environment Setup
      • Logging
      • Metrics
      • NextJS Custom Server
      • Observability
      • Operations Base Principles
      • Security
      • Service Configuration
      • Support
    • AWS Secrets
    • Feature Flags
    • Documentation Contributions
    • Defining Monorepo Boundaries With Tags
    • OpenAPI
    • Code Generation
    • Workspace Settings (Deprecated)
    • External Contributions
  • REFERENCE
    • Problems
      • 400 Validation Failed
      • 400 Attempt Failed
      • 403 Bad Subject
      • 400 500 Template API Error
    • Glossary
  • Misc
    • Guide: Adding a Payment Step to an Application
    • Guide: Enable Organisations to Make Requests to an Application
    • README Template
Powered by GitBook
On this page
  • Case styles
  • Examples
  • Input objects naming conventions
  • Query naming conventions
  • Fetching array of items
  • Mutation naming conventions
  • Integrating naming conventions into shared api
  • Solution
  • Conclusion

Was this helpful?

  1. Technical overview
  2. API Design Guide

GraphQL Naming Conventions

This document describes the GraphQL's Naming Conventions

Case styles

  • Field names should use camelCase. Many GraphQL clients are written in JavaScript, Java, Kotlin, or Swift, all of which recommend camelCase for variable names.

  • Type names should use PascalCase. This matches how classes are defined in the languages mentioned above.

  • Enum names should use PascalCase.

  • Enum values should use ALL_CAPS, because they are similar to constants.

Examples

type Dog {
  id: String!
}

query DogQuery {
  dog {
    id
  }
}

enum Animal {
  DOG
  CAT
}

Input objects naming conventions

Use a single, required, unique, input object type as an argument for easier execution on the client.

query DogQuery {
  dog(input: DogQueryInput!) {
    id
  }
}

mutation PetDogMutation {
  petDog(input: PetDogInput!) {
    id
  }
}

Query naming conventions

GraphQL is about asking for specific fields on objects, therefore it's essential that the query has exactly the same shape as the result. Naming queries the same as the type will help with that:

query {
  dog {
    id
  }
}

will result in:

data {
  dog {
    id
  }
}

You can make a sub-selection of fields for that object. GraphQL queries can traverse related objects and their fields.

query {
  dog {
    id
    owner {
      id
    }
  }
}

Fetching array of items

GraphQL list queries work quite differently from single field queries. We know which one to expect based on what is indicated in the schema, which will be denoted by a plural name.

The pagination structure should follow a simplified version of the Relay's connection pattern.

query {
  dog {
    id
    owners(first: 10, after: $endCursor) {
      count
      items {
        id
      }
      pageInfo {
        hasNextPage
        endCursor
      }
    }
  }
}

Mutation naming conventions

Make mutations as specific as possible. Mutations should represent semantic actions that might be taken by the user whenever possible.

Name your mutations verb first. Then the object, or “noun,” if applicable; createAnimal is preferable to animalCreate.

mutation {
  createAnimal(input: CreateAnimalInput!) {
    id
  }
}

Integrating naming conventions into shared api

Now for the hard part!

Since all of our resolvers will be merged into a shared api, we need to come up with a system to avoid overwriting previous resolvers. Let's take this example to understand the problem better.

We have two projects, ProjectX and ProjectY. Both of these projects need to query a user, but the user type references a different resource.

  1. ProjectX's resolvers are merged first with the user query that fetches users from the National Registry.

  2. ProjectY's resolvers are merged after with the user query that fetches users from the RSK.

Here the ProjectY's user query will overwrite the ProjectX's query and will break ProjectX.

Solution

We need to prefix all Mutations, Queries, Types, Scalars, etc. with the name of the module that is merged into the api.

Mutations should still follow the verb first rule. Then the module name, following the object, or “noun,” if applicable.

Exception to the solution

If the name of the GraphQL type/query/mutation is the same as the module, then we don't need the prefix.

Example

type ProjectXUser {
  id: String!
}

query {
  projectXUser {
    id
  }
}

mutation {
  createProjectXUser {
    id
  }
}
# here we are working in the identity module, thus we won't name our type IdentityIdenty. Same exception applies for query/mutation

type Identity {
  id: UUID!
}

query {
  identity {
    id
  }
}

mutation {
  createIdentity {
    id
  }
}

Conclusion

With these design principles you should be equipped to design an effective GraphQL system for your API.

PreviousExample API ServiceNextMethods

Last updated 2 years ago

Was this helpful?