graphql typedef code example

Example 1: apollo server build schema passing parameters to classes

// https://github.com/MichalLytek/type-graphql/blob/master/docs/dependency-injection.md

import { buildSchema } from "type-graphql";
// import your IoC container
import { Container } from "typedi";

import { SampleResolver } from "./resolvers";

// build the schema as always
const schema = await buildSchema({
  resolvers: [SampleResolver],
  // register the 3rd party IOC container
  container: Container,
});

Example 2: graphql nested schema

const StaffType = new GraphQLObjectType({
    name: 'Staff',
    fields: {
      id: {type: GraphQLInt},
      name: {type: GraphQLString},
      role: {type: GraphQLString},
      address: {type: AddressType}
    }
})

const AddressType = new GraphQLObjectType({
    name: 'Address',
    fields: {
      street: {type: GraphQLString},
      town: {type: GraphQLString}
    }
})

Tags:

Sql Example