javascriptgraphql

Import build schema from .graphql file


I want to create a GraphQL API with the following schema

app.use(
  "/graphql",
  graphQlHttp({
    schema: buildSchema(`
    type Event {
        _id: ID!
        title: String!
        description: String!
        price: Float!
    }

    input EventInput {
        title: String!
        description: String!
        price: Float!
    }

    type QueryResolver {
        getEvents: [Event!]!
    }

    type MutationResolver {
        createEvent(eventInput: EventInput): [Event!]!
    }

    schema {
        query: QueryResolver
        mutation: MutationResolver
    }
    `),
    rootValue: {}
  })
);

Currently I am using it as a string in in the my main file. I want to separate it out in a graphql file. One way can be that we can read the file but I think it will not be efficient.

Can you please tell me how can I do this?


Solution

  • Just import all of your schemas using the gql module. Install it by:

    npm i graphql-tag