ardatan/graphql-import

Ability to exclude Query, Mutation and Subscription types from imported schema

Opened this issue · 6 comments

atten commented

This feature would be useful when we have types and inputs generated by Prisma, and want to add custom implemented Query and Mutation for real-world app.

For example:

const { importSchema } = require("graphql-import");
const { GraphQLServer } = require('graphql-yoga');
const { mergeTypes } = require("merge-graphql-schemas");

const typeDefs = mergeTypes([
  importSchema(`# import * from './src/graphql/generated/prisma.graphql'`),
  readFileSync('./src/common/schema.graphqls', {encoding: 'utf-8'}),
  readFileSync('./src/auth/schema.graphqls', {encoding: 'utf-8'}),
  ...
], { all: true });

const server = new GraphQLServer({
  typeDefs,
  ...
});

But now I can't do that because importSchema grabs generated Query and Mutation types directly into application API.

atten commented

I've made a fork, that works exactly as I need.

It has one difference in index.ts:

  document.definitions = completeDefinitionPool(
    flatten(allDefinitions),
    [],  // empty array instead of "firstSet"
    flatten(typeDefinitions),
  )

@atten I think I'm right in saying that if the imported file has any duplicate type (including type Query) it won't import the duplicate. So if you merged your readFileSync lines and then unshift on the import, I think you get this for free.

atten commented

@Siyfion unfornunately, graphql-import does import duplicates.

I've tried to transform import like this, but got multiple errors
Field Mutation.%s can only be defined once.

let typeDefs = mergeTypes([
  readFileSync('./src/common/schema.graphqls', {encoding: 'utf-8'}),
  readFileSync('./src/auth/schema.graphqls', {encoding: 'utf-8'}),
 ...
], { all: true });

typeDefs = importSchema(typeDefs + `
  # import * from './src/graphql/generated/prisma.graphql'
  `
);

@atten I've done exactly this and it's working fine...? :/
The only thing I can see different is that I have my # import lines at the top of my SDL file.