graphql / graphql-js

A reference implementation of GraphQL for JavaScript
http://graphql.org/graphql-js/
MIT License
20.02k stars 2.02k forks source link

`buildType` function #597

Open langpavel opened 7 years ago

langpavel commented 7 years ago

Why?

You can write entire schema in GraphQL and then call buildSchema. But you cannot write only single type or partials and then combine them simply. Why?

There is some effort:

Example: contentType.graphql:

type ContentType {
  path: String!
  title: String!
  perex: String!
}

Then with today tooling it should be possible to write:

import { ContentType } from './types/contentType.graphql';

export default function createSchema(context) {
  return new Schema({
    query: new ObjectType({
      name: 'Query',
      fields: {
        content: {
          type: ContentType,
          args: {
            path: { type: new NonNull(StringType) },
          },
          async resolve({ request }, { path, locale }) {
            const { success, fileName, extension } = await resolveFileName(path, locale);
            if (!success) {
              return null;
            }

            const source = await readFile(fileName, { encoding: 'utf8' });
            return parseContent(path, source, extension);
          },
        }
      },
  });
}

Goal

Write modules containing types in GraphQL (*.graphql files), then compose this pieces in meaningful schema in your app.

As you see, you can mix pure GraphQL with custom field resolvers — this is only goal of mine.

Now-a-day it is piece of cake to optimize all the things using require/babel/webpack plugins..

@leebyron Can you help me with this a little? What is the right way for modular GraphQL backends? :-)

migueloller commented 7 years ago

The discussion for graphql-utilities is here: #474

IvanGoncharov commented 5 years ago

It's a pretty big change that should go through a series of RC to polish interface and ensure it covers the majority of use cases. So it should be planned for 15.0.0