tyranid-graphql
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "tyranid-graphql", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "GraphQL plugin for tyranid", | ||
"main": "dist-node4/index.js", | ||
"scripts": { | ||
"start": "npm run build && node ./dist-node4/test/example/server.js", | ||
"babel": "./node_modules/.bin/babel ./dist-es2015 --out-dir ./dist-node4", | ||
@@ -19,4 +20,8 @@ "build": "rm -rf ./dist-es2015/ ./dist-node4/ && npm run lint && npm run tsc && npm run babel", | ||
"devDependencies": { | ||
"@types/body-parser": "0.0.32", | ||
"@types/express": "^4.0.32", | ||
"@types/hapi": "^13.0.34", | ||
"@types/koa": "^2.0.32", | ||
"@types/mongodb": "^2.1.32", | ||
"apollo-server": "^0.2.8", | ||
"ava": "^0.16.0", | ||
@@ -26,3 +31,6 @@ "babel-cli": "^6.14.0", | ||
"babel-preset-es2015-node4": "^2.1.0", | ||
"body-parser": "^1.15.2", | ||
"codecov": "^1.0.1", | ||
"express": "^4.14.0", | ||
"graphql-tools": "^0.7.2", | ||
"mongodb": "^2.2.10", | ||
@@ -29,0 +37,0 @@ "nyc": "^8.3.0", |
@@ -1,2 +0,2 @@ | ||
## GraphQL frontend for Tyranid [![Build Status](https://travis-ci.org/tyranid-org/tyranid-graphql.svg?branch=master)](https://travis-ci.org/tyranid-org/tyranid-graphql) [![codecov](https://codecov.io/gh/tyranid-org/tyranid-graphql/branch/master/graph/badge.svg)](https://codecov.io/gh/tyranid-org/tyranid-graphql) | ||
## GraphQL frontend for Tyranid [![npm version](https://badge.fury.io/js/tyranid-graphql.svg)](https://badge.fury.io/js/tyranid-graphql) [![Build Status](https://travis-ci.org/tyranid-org/tyranid-graphql.svg?branch=master)](https://travis-ci.org/tyranid-org/tyranid-graphql) [![codecov](https://codecov.io/gh/tyranid-org/tyranid-graphql/branch/master/graph/badge.svg)](https://codecov.io/gh/tyranid-org/tyranid-graphql) | ||
@@ -37,3 +37,20 @@ This library adds a graphql query interpreter for the [`tyranid`](https://github.com/tyranid-org/tyranid) | ||
## Interactive graphql server Example | ||
![Apollo server example](/test/example/example.gif) | ||
To see an example of `apollo-server` (http://dev.apollodata.com/core/) | ||
being used with a generated schema from tyranid, clone the repo, install deps... | ||
```shell | ||
npm install | ||
``` | ||
and run the example server | ||
```shell | ||
npm start | ||
``` | ||
## Detailed Code Example | ||
@@ -40,0 +57,0 @@ |
@@ -54,11 +54,14 @@ import { Tyr } from 'tyranid'; | ||
export function graphqlize(tyr: typeof Tyr) { | ||
const schema = createGraphQLSchema(tyr.collections); | ||
const schema = createGraphQLSchema(tyr); | ||
tyr.graphql = function({ query, auth, perm = 'view' }) { | ||
return graphql(schema, query, null, { | ||
auth, | ||
perm, | ||
docCache: {} | ||
}); | ||
}; | ||
tyr.graphql = <Tyr.TyranidGraphQLFunction> Object.assign( | ||
function({ query, auth, perm = 'view' }: Tyr.TyranidGraphQlQueryOptions) { | ||
return graphql(schema, query, null, { | ||
auth, | ||
perm, | ||
docCache: {} | ||
}); | ||
}, | ||
{ schema } | ||
); | ||
} | ||
@@ -75,7 +78,7 @@ | ||
*/ | ||
export function createGraphQLSchema(collections: Tyr.TyranidCollectionList) { | ||
export function createGraphQLSchema(tyr: typeof Tyr) { | ||
const typeMap: GraphQLOutputTypeMap = new Map(); | ||
const queryFields: GraphQLFieldConfigMap = {}; | ||
collections.forEach(col => { | ||
tyr.collections.forEach(col => { | ||
const name = col.def.name; | ||
@@ -82,0 +85,0 @@ if (!col.def.fields) throw new TypeError(`Collection "${name}" has no fields!`); |
import { Tyr } from 'tyranid'; | ||
import { GraphQLResult } from 'graphql'; | ||
import { GraphQLResult, GraphQLSchema } from 'graphql'; | ||
@@ -14,7 +14,12 @@ declare module 'tyranid' { | ||
export interface TyranidGraphQLFunction { | ||
(opts: TyranidGraphQlQueryOptions): Promise<GraphQLResult>; | ||
schema: GraphQLSchema; | ||
} | ||
// add graphql method to tyranid module | ||
// TODO: should be strongly typed, returning wrapped docs! | ||
export function graphql(opts: TyranidGraphQlQueryOptions): Promise<GraphQLResult>; | ||
export let graphql: TyranidGraphQLFunction; | ||
} | ||
} |
@@ -23,2 +23,3 @@ { | ||
"./src/index.ts", | ||
"./test/example/server.ts", | ||
"./node_modules/typed-graphql/graphql.d.ts", | ||
@@ -25,0 +26,0 @@ "./src/tyranid-extensions.d.ts" |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
53517
906
201
20
1