Can't get Graphql-tools to read my schema.graphql file

4.1k Views Asked by At

I am using Apollo-server-express and Graphql-tools. I have been all over the Graphql-tools documentation and I can't get this to work. I'm trying to get my schema.graphql file to import as my typeDefs. It seems like Graphql-tools should be making this easy, but something isn't falling into place.

index.js

const { ApolloServer } = require("apollo-server-express");
const { makeExecutableSchema } = require('@graphql-tools/schema');
const express = require("express");
const { join } = require("path");
const { loadSchema } = require("@graphql-tools/load");
const { GraphQLFileLoader } = require("@graphql-tools/graphql-file-loader");
const { addResolversToSchema } = require("@graphql-tools/schema");
const app = express();

const resolvers = {
  Query: {
    items: (parent, args, ctx, info) => {
      return ctx.prisma.item.findMany();
    },
  },
  Mutation: {
    makeItem: (parent, args, context, info) => {
      const newItem = context.prisma.item.create({
        data: {
          ...args,
          price: parseInt(Math.ceil(args.price * 100)),
        },
      });
      return newItem;
    },
    deleteItem: (parent, args, context, info) => {
      return context.prisma.item.delete({
        where: {
          id: args.id,
        },
      });
    },
  },
};

const schemaSource = loadSchemaSync(join(__dirname, "schema.graphql"), {
  loaders: [new GraphQLFileLoader()],
});

const schema = makeExecutableSchema({
  typeDefs: schemaSource,
  resolvers,
});

const server = new ApolloServer({
  schema,
  resolvers,
});

server.applyMiddleware({ app });

app.listen(
  { port: 4000 },
  () =>
    console.log(
      ` => Backend server is now running on port http://localhost:4000`
    )
);

schema.graphql

type Query {
  items: [Item!]!
}

type Mutation {
  makeItem(
    piece: String!
    image: String!
    identifier: String!
    price: Float!
    itemNumber: Int!
  ): Item!
  deleteItem(id: ID!): Item!
}

type Item {
  id: ID!
  piece: String!
  image: String!
  identifier: String!
  price: Int!
  itemNumber: Int!
}

In its current state I am getting an error that says: "Error: typeDefs must be a string, array or schema AST, got object"

As I understand it makeExecutableSchema should be doing all the necessary steps, like changing the schema into a string. I can't seem to figure out what is going on here and any help would be greatly appreciated.

1

There are 1 best solutions below

6
On BEST ANSWER

loadSchemaSync will load a GraphQLSchema object using the provided pointer. You should use loadTypedefsSync instead.

const sources = loadTypedefsSync(join(__dirname, "schema.graphql"), {
  loaders: [new GraphQLFileLoader()],
});
const typeDefs = sources.map(source => source.document)
const server = new ApolloServer({ typeDefs, resolvers })

If you want to use loadSchema, you don't need to use makeExecutableSchema because your schema has already been created. So you would do this instead as shown in the docs:

const schema = loadSchemaSync(join(__dirname, "schema.graphql"), {
  loaders: [new GraphQLFileLoader()],
});

const resolvers = {...};
const schemaWithResolvers = addResolversToSchema({
    schema,
    resolvers,
});
const server = new ApolloServer({ schema: schemaWithResolvers })