graphql / graphql/graphql-js

`buildType` function

Open
#597 2 comments 4 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
20.3k
Forks
2.1k
Avg merge
44m
Merged PRs (30d)
6

Description

## 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:
* `graphql/utilities` - `buildASTSchema`, `extendSchema`, `concatAST` but it is not clear how to use properly
* [`graphql-utilities`](https://github.com/bloveit/graphql-utilities)
* [`graphql-tools`](https://github.com/apollostack/graphql-tools)

## Example: `contentType.graphql`:
```graphql
type ContentType {
path: String!
title: String!
perex: String!
}
```

Then with today tooling it should be possible to write:

```js
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? :-)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.