ardatan/graphql-import

Problem with import a schema.graphql

Closed this issue · 4 comments

I can't import my schema.graphql file in typeDefs on my schema.js

//* Imports
import { importSchema } from 'graphql-import';
import { makeExecutableSchema } from 'graphql-tools';
import { resolvers } from './resolvers';

// Importar Schemas
const typeDefs = importSchema('data/schema.graphql');
const schema   = makeExecutableSchema({typeDefs, resolvers});

export { schema };

Here is my code (schema.js) looks so good, but in my terminal, I see the next error

C:\Users\ID139\Documents\Projects\support-graphql\node_modules\graphql-tools\dist\generate\buildSchemaFromTypeDefinitions.js:15
throw new _1.SchemaError("typeDefs must be a string, array or schema
AST, got " + type);
^

[Error: typeDefs must be a string, array or schema AST, got object]
[nodemon] app crashed - waiting for file changes before starting...

But if I copy all types of my schema.graphql into a template string on the variable typeDefs the code its work again

importSchema returns promise so you need to resolve it.

I'm new to GraphQL, I have the following:

import { GraphQLServer } from "graphql-yoga";
import { importSchema } from "graphql-import";

const typeDefs = importSchema("./src/schema/schema.graphql");

const createServer = () =>
  new GraphQLServer({
    typeDefs,
    resolvers: {
      Mutation,
      Query
    },
    resolverValidationOptions: {
      requireResolversForResolveType: false
    },
    context: req => ({ ...req, db })
  });

I figured that importSchema return a promise, but I have no idea how to use it in this context? Any idea how to resolve this I would really appreciate it.

You can just do the following

import { GraphQLServer } from "graphql-yoga";
import { importSchema } from "graphql-import";

const createServer = async () =>
  new GraphQLServer({
    typeDefs: await importSchema("./src/schema/schema.graphql"),
    resolvers: {
      Mutation,
      Query
    },
    resolverValidationOptions: {
      requireResolversForResolveType: false
    },
    context: req => ({ ...req, db })
  });

importSchema returns a string like before. So you won't have that problem after 1.0.0. Feel free to open a new issue if you still have the same problem.