Skip to main content

GraphQL

Introduction

CairJS use GraphQL, as query language for APIs.

Install package

npm install @cairjs/graphql

Apollo Server

We use Apollo Server as a GraphQL server to expose the necessary resources that the Client wants to consume.

Start Server

index.ts
import { ApolloServer } from '@cairjs/graphql'

const typeDefs = //...
const resolvers = //...

const server = new ApolloServer({ typeDefs, resolvers })

server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})

Define a Query resolver

types

  • string: .
  • boolen: .
  • number: .
  • ID: .
  • json: .
  • upload: .

array type

[type]

Example

import { resolver } from '@cairjs/graphql'

export default resolver({
type: 'string',
args: {
name: {
type: 'string'
}
},
resolve: async (args, context) => {
return args.name
}
})