Skip to content

Commit 96cbac3

Browse files
Merge pull request #64 from orta/ex_schema
Add support + docs for exporting the GraphQLSchema
2 parents 889f134 + 2844c4b commit 96cbac3

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,26 @@ app.use('/graphql', jsonGraphqlExpress(data));
530530
app.listen(PORT);
531531
```
532532

533+
## Schema Export
534+
535+
You can also use the export `jsonSchemaBuilder` to get your own copy of the GraphQLSchema:
536+
537+
In node:
538+
```js
539+
import {graphql} from 'graphql';
540+
import {jsonSchemaBuilder} from 'json-graphql-server';
541+
542+
const data = { };
543+
const schema = jsonSchemaBuilder(data);
544+
const query = `[...]`
545+
546+
graphql(schema, query).then(result => {
547+
console.log(result);
548+
});
549+
```
550+
551+
Or available in the global scope when running on a client as `jsonSchemaBuilder`.
552+
533553
## Deployment
534554

535555
Deploy with Heroku or Next.js.

src/client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import GraphQLClientServer from './graphQLClientServer';
2+
import schemaBuilder from './schemaBuilder';
23

34
if (typeof window !== 'undefined') {
45
window.JsonGraphqlServer = GraphQLClientServer;
6+
window.jsonSchemaBuilder = schemaBuilder;
57
}
68

79
export default GraphQLClientServer;

src/jsonGraphqlExpress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import schemaBuilder from './schemaBuilder';
99
*
1010
* @example
1111
* import express from 'express';
12-
* import { jsonGraphqlExpress } from 'json-graphql-server';
12+
* import jsonGraphqlExpress from 'json-graphql-server';
1313
*
1414
* const data = {
1515
* "posts": [

src/node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import jsonGraphqlExpress from './jsonGraphqlExpress';
2+
import schemaBuilder from './schemaBuilder';
23

4+
export const jsonSchemaBuilder = schemaBuilder;
35
export default jsonGraphqlExpress;

src/schemaBuilder.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,50 @@ import { printSchema } from 'graphql';
33
import getSchemaFromData from './introspection/getSchemaFromData';
44
import resolver from './resolver';
55

6+
/**
7+
* Generates a GraphQL Schema object for your data
8+
*
9+
* @param {any} data
10+
* @returns A GraphQL Schema
11+
*
12+
* @example
13+
* import {graphql} from 'graphql';
14+
* import {jsonSchemaBuilder} from 'json-graphql-server';
15+
*
16+
* const data = {
17+
* "posts": [
18+
* {
19+
* "id": 1,
20+
* "title": "Lorem Ipsum",
21+
* "views": 254,
22+
* "user_id": 123,
23+
* },
24+
* {
25+
* "id": 2,
26+
* "title": "Sic Dolor amet",
27+
* "views": 65,
28+
* "user_id": 456,
29+
* },
30+
* ],
31+
* "users": [
32+
* {
33+
* "id": 123,
34+
* "name": "John Doe"
35+
* },
36+
* {
37+
* "id": 456,
38+
* "name": "Jane Doe"
39+
* }
40+
* ],
41+
* };
42+
*
43+
* const schema = jsonSchemaBuilder(data);
44+
* const query = `[...]`
45+
* graphql(schema, query).then(result => {
46+
* console.log(result);
47+
* });
48+
*
49+
*/
650
export default data =>
751
makeExecutableSchema({
852
typeDefs: printSchema(getSchemaFromData(data)),

0 commit comments

Comments
 (0)