-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
v1rtl
committed
Mar 26, 2021
1 parent
4fa301d
commit 7f40534
Showing
7 changed files
with
108 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,23 +23,29 @@ The simplest setup with `std/http`: | |
```ts | ||
import { serve } from 'https://deno.land/[email protected]/http/server.ts' | ||
import { GraphQLHTTP } from 'https://deno.land/x/gql/mod.ts' | ||
import { buildSchema } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { makeExecutableSchema } from 'https://deno.land/x/graphql_tools/mod.ts' | ||
import { gql } from 'https://deno.land/x/graphql_tag/mod.ts' | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
const typeDefs = gql` | ||
type Query { | ||
hello: String | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: () => `Hello World!` | ||
} | ||
} | ||
`) | ||
|
||
const schema = makeExecutableSchema({ resolvers, typeDefs }) | ||
|
||
const s = serve({ port: 3000 }) | ||
|
||
for await (const req of s) { | ||
req.url.startsWith('/graphql') | ||
? await GraphQLHTTP({ | ||
schema, | ||
rootValue: { | ||
hello: () => 'Hello World!' | ||
}, | ||
graphiql: true | ||
})(req) | ||
: req.respond({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { graphql, GraphQLSchema, ExecutionResult } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { GraphQLArgs } from 'https://deno.land/x/[email protected]/lib/graphql.d.ts' | ||
|
||
export type GraphQLOptions<Context = any, Request = any> = { | ||
export interface GraphQLOptions<Context = any, Request = any> extends Omit<GraphQLArgs, 'source'> { | ||
schema: GraphQLSchema | ||
context?: (val: Request) => Context | Promise<Context> | ||
rootValue?: any | ||
graphiql?: boolean | ||
} | ||
interface Params { | ||
|
@@ -34,7 +34,7 @@ export type GraphQLParams = QueryParams | MutationParams | |
* const { errors, data } = await runHttpQuery<ServerRequest, typeof context>({ query: `{ hello }` }, { schema } }, context) | ||
* ``` | ||
*/ | ||
export async function runHttpQuery<Req extends any = any, Context extends { request: Req } = { request: Req }>( | ||
export async function runHttpQuery<Req extends any = any, Context = { request?: Req }>( | ||
params: GraphQLParams, | ||
options: GraphQLOptions<Context, Req>, | ||
context?: Context | any | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import { Application } from 'https://deno.land/x/oak/mod.ts' | ||
import { GraphQLHTTP } from '../mod.ts' | ||
import { buildSchema } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { makeExecutableSchema } from 'https://deno.land/x/graphql_tools/mod.ts' | ||
import { gql } from 'https://deno.land/x/graphql_tag/mod.ts' | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
const typeDefs = gql` | ||
type Query { | ||
hello: String | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: () => `Hello World!` | ||
} | ||
} | ||
`) | ||
|
||
const schema = makeExecutableSchema({ resolvers, typeDefs }) | ||
|
||
const app = new Application() | ||
|
||
app.use((ctx) => | ||
GraphQLHTTP({ | ||
schema, | ||
graphiql: true, | ||
rootValue: { | ||
hello: () => `Hello World!` | ||
} | ||
graphiql: true | ||
})(ctx.request.serverRequest) | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import { opine } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { GraphQLHTTP } from '../mod.ts' | ||
import { buildSchema } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { makeExecutableSchema } from 'https://deno.land/x/graphql_tools/mod.ts' | ||
import { gql } from 'https://deno.land/x/graphql_tag/mod.ts' | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
const typeDefs = gql` | ||
type Query { | ||
hello: String | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: () => `Hello World!` | ||
} | ||
} | ||
`) | ||
|
||
const schema = makeExecutableSchema({ resolvers, typeDefs }) | ||
|
||
const app = opine() | ||
|
||
app | ||
.use( | ||
'/graphql', | ||
GraphQLHTTP({ | ||
schema, | ||
rootValue: { | ||
hello: () => 'Hello World!' | ||
} | ||
}) | ||
) | ||
.listen(3000, () => console.log(`☁ Started on http://localhost:3000`)) | ||
app.use('/graphql', GraphQLHTTP({ schema })).listen(3000, () => console.log(`☁ Started on http://localhost:3000`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
import { App } from 'https://deno.land/x/tinyhttp/mod.ts' | ||
import { GraphQLHTTP } from '../mod.ts' | ||
import { buildSchema } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { makeExecutableSchema } from 'https://deno.land/x/graphql_tools/mod.ts' | ||
import { gql } from 'https://deno.land/x/graphql_tag/mod.ts' | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
const typeDefs = gql` | ||
type Query { | ||
hello: String | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: () => `Hello World!` | ||
} | ||
} | ||
`) | ||
|
||
const schema = makeExecutableSchema({ resolvers, typeDefs }) | ||
|
||
const app = new App() | ||
|
||
|
@@ -15,10 +24,7 @@ app | |
'/graphql', | ||
GraphQLHTTP({ | ||
schema, | ||
graphiql: true, | ||
rootValue: { | ||
hello: () => 'Hello World!' | ||
} | ||
graphiql: true | ||
}) | ||
) | ||
.listen(3000, () => console.log(`☁ Started on http://localhost:3000`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import { serve } from 'https://deno.land/[email protected]/http/server.ts' | ||
import { GraphQLHTTP } from '../mod.ts' | ||
import { buildSchema } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { makeExecutableSchema } from 'https://deno.land/x/graphql_tools/mod.ts' | ||
import { gql } from 'https://deno.land/x/graphql_tag/mod.ts' | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
const typeDefs = gql` | ||
type Query { | ||
hello: String | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: () => `Hello World!` | ||
} | ||
} | ||
`) | ||
|
||
const schema = makeExecutableSchema({ resolvers, typeDefs }) | ||
|
||
const s = serve({ port: 3000 }) | ||
|
||
for await (const req of s) { | ||
req.url.startsWith('/graphql') | ||
? await GraphQLHTTP({ | ||
schema, | ||
rootValue: { | ||
hello: () => 'Hello World!' | ||
}, | ||
graphiql: true | ||
})(req) | ||
: req.respond({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
import { superdeno } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { GraphQLHTTP } from '../http.ts' | ||
import { runHttpQuery } from '../common.ts' | ||
import { GraphQLSchema, GraphQLString, GraphQLObjectType } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { buildSchema } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { describe, it, run, expect } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { ServerRequest } from 'https://deno.land/[email protected]/http/server.ts' | ||
|
||
const schema = new GraphQLSchema({ | ||
query: new GraphQLObjectType({ | ||
name: 'Query', | ||
fields: { | ||
hello: { | ||
type: GraphQLString, | ||
resolve() { | ||
return 'Hello World!' | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
} | ||
`) | ||
|
||
const app = GraphQLHTTP({ schema }) | ||
const rootValue = { | ||
hello: () => 'Hello World!' | ||
} | ||
|
||
const app = GraphQLHTTP({ schema, rootValue }) | ||
|
||
describe('GraphQLHTTP(opts)', () => { | ||
it('should send 405 on GET', async () => { | ||
|
@@ -42,21 +38,14 @@ describe('GraphQLHTTP(opts)', () => { | |
}) | ||
it('should pass req obj to server context', async () => { | ||
type Context = { request: ServerRequest } | ||
const schema = new GraphQLSchema({ | ||
query: new GraphQLObjectType<unknown, Context>({ | ||
name: 'Query', | ||
fields: { | ||
hello: { | ||
type: GraphQLString, | ||
resolve(_1, _2, { request }) { | ||
return `Request from ${request.url}` | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
const app = GraphQLHTTP<ServerRequest, Context>({ | ||
schema, | ||
fieldResolver: (_, __, ctx: Context, info) => { | ||
if (info.fieldName === 'hello') { | ||
return `Request from ${ctx.request.url}` | ||
} | ||
}, | ||
context: (request) => ({ request }) | ||
}) | ||
|
||
|
@@ -75,7 +64,7 @@ describe('runHttpQuery(params, options, context)', () => { | |
{ | ||
query: '{ hello }' | ||
}, | ||
{ schema } | ||
{ schema, rootValue } | ||
) | ||
|
||
expect(result.data).toEqual({ hello: 'Hello World!' }) | ||
|
@@ -97,24 +86,25 @@ describe('runHttpQuery(params, options, context)', () => { | |
it('should use properties passed to context', async () => { | ||
const obj = { a: 'Context prop' } | ||
|
||
const schema = new GraphQLSchema({ | ||
query: new GraphQLObjectType<any, typeof obj>({ | ||
name: 'Query', | ||
fields: { | ||
hello: { | ||
type: GraphQLString, | ||
resolve(_, __x, ctx) { | ||
return ctx.a | ||
} | ||
const result = await runHttpQuery<unknown, typeof obj>( | ||
{ query: '{ hello }' }, | ||
{ | ||
schema, | ||
fieldResolver: (_, __, ctx: typeof obj, info) => { | ||
if (info.fieldName === 'hello') { | ||
return ctx.a | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
const result = await runHttpQuery({ query: '{ hello }' }, { schema }, obj) | ||
}, | ||
obj | ||
) | ||
|
||
expect(result.data).toEqual({ hello: 'Context prop' }) | ||
}) | ||
}) | ||
|
||
describe('GraphQL playground', () => { | ||
it('should send method not allowed if playground is disabled', () => {}) | ||
}) | ||
|
||
run() |