merge different schema in graphql

4.8k views Asked by At

schema.js

const { buildSchema } = require('graphql');

module.exports = buildSchema(`
type Booking {
    _id: ID!
    event: Event!
    user: User!
}

type Event {
    _id: ID!
    title: String!
    description: String!
    price: Float!
    creator: User
}

type User {
    _id: ID!
    email: String!
    password: String
    createdEvents: [Event!]
}
type RootQuery {
    events: [Event!]!
    users: [User!]!
    bookings: [Booking!]!
}

schema {
    query: RootQuery
}

`);

index.js

app.use(
'/graphql',
graphqlHTTP({
    schema: graphQlSchema,
    rootValue: graphQlResolvers,
    graphiql: true
})
);

I am just trying to learn graphql and it's my first time so I am a little bit confused above query is working fine but what I want 3 different files for booking, user, event and merge them in a single file name index.js and after that import in the main index.js which is above one. This is the first time I am learning graphql. Any help will be appreciated

1

There are 1 answers

0
Lin Du On BEST ANSWER

You could use Type definitions (SDL) merging of graphql-tools package to merge your type definition files.

This tools merged GraphQL type definitions and schema. It aims to merge all possible types, interfaces, enums and unions, without conflicts.

E.g.

types/booking.js:

module.exports = `
type Booking {
    _id: ID!
    event: Event!
    user: User!
}
`;

types/user.js:

module.exports = `
type User {
    _id: ID!
    email: String!
    password: String
    createdEvents: [Event!]
}
`;

types/event.js:

module.exports = `
type Event {
    _id: ID!
    title: String!
    description: String!
    price: Float!
    creator: User
}
`;

types/index.js:

const { mergeTypeDefs } = require('@graphql-tools/merge');
const bookingType = require('./booking');
const userType = require('./user');
const eventType = require('./event');

const rootTypes = `
type RootQuery {
    events: [Event!]!
    users: [User!]!
    bookings: [Booking!]!
}

schema {
    query: RootQuery
}
`;

const types = [bookingType, userType, eventType, rootTypes];

module.exports = mergeTypeDefs(types);

server.js:

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema, print } = require('graphql');
const typeDefs = require('./types');

const app = express();
const port = 4000;
const schema = buildSchema(print(typeDefs));
console.log(print(typeDefs));

app.use(
  '/graphql',
  graphqlHTTP({
    schema,
    graphiql: true,
  }),
);

app.listen(port, () => console.log('Server started at port:', port));

Server logs:

type Booking {
  _id: ID!
  event: Event!
  user: User!
}

type User {
  _id: ID!
  email: String!
  password: String
  createdEvents: [Event!]
}

type Event {
  _id: ID!
  title: String!
  description: String!
  price: Float!
  creator: User
}

type RootQuery {
  events: [Event!]!
  users: [User!]!
  bookings: [Booking!]!
}

schema {
  query: RootQuery
}

Server started at port: 4000