ardatan / graphql-import

Import & export definitions in GraphQL SDL
868 stars 56 forks source link

Proposal: Automatic imports #130

Open timsuchanek opened 6 years ago

timsuchanek commented 6 years ago

When using graphql-import together with a Prisma Service, a common situation that a developer finds herself in is copying over type names.

This happens when you just want to "forward" the underlying service types to the resulting application schema.

While the current explicit nature of graphql-import provides good readability and maintainability, it requires in certain cases a lot of boilerplate code.

To make it possible for developers to have quicker iterations on schemas, we should introduce an automatic import mechanism.

That means, that any type used in the resulting schema, that is not defined, will be searched in specified source files.

The API could look like this:

const typeDefs = importSchema('schema.graphql', ['./generated/source.graphql'])

schema.graphql

type A {
  # test 1
  first: String
  second: Float
  b: B
}

./generated/source.graphql

type B {
  hello: String!
}

Anyone interested working on this, please answer in this issue or join our public Graphcool slack!

mxmzb commented 6 years ago

I like this very much. You could simplify the API even further and just pass multiple files to importSchema without telling what's the main file. It would be simply in the obligation to pass the files in the correct order.

For this to work I also imagine would be kind of easy and could eliminate the need for comment imports from the .graphql files themselves: Just read the file contents and join them together before parsing as graphql.

SpaceK33z commented 6 years ago

This is certainly interesting for me, since in the app I'm building the comment imports are getting bigger and bigger.

Often you don't want everything from the imported schema, see for example this:

schema.graphql

type User {
  id: ID!
  email: String!
  # The `password` field is purposefully left out here, since we want to hide it!
}

./generated/prisma.graphql

type Query {
  user: User
  users: [User!]
}

type User {
  id: ID!
  email: String!
  password: String!
}

So with those two files as input, what would be the output? In the usecases I've encountered most so far with Prisma I'd really hope it to be this:

output.graphql

type Query {
  user: User
  users: [User!]
}

type User {
  id: ID!
  email: String!
}

I hope I didn't misunderstand this proposal 😄. It could be really powerful!

Also related to this is #51.