Skip to content

Commit

Permalink
update tests & examples
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Mar 26, 2021
1 parent 4fa301d commit 7f40534
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 95 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions common.ts
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 {
Expand Down Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions examples/oak.ts
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)
)

Expand Down
31 changes: 15 additions & 16 deletions examples/opine.ts
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`))
24 changes: 15 additions & 9 deletions examples/tinyhttp.ts
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()

Expand All @@ -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`))
22 changes: 14 additions & 8 deletions examples/vanilla.ts
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({
Expand Down
74 changes: 32 additions & 42 deletions test/mod.test.ts
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 () => {
Expand All @@ -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 })
})

Expand All @@ -75,7 +64,7 @@ describe('runHttpQuery(params, options, context)', () => {
{
query: '{ hello }'
},
{ schema }
{ schema, rootValue }
)

expect(result.data).toEqual({ hello: 'Hello World!' })
Expand All @@ -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()

0 comments on commit 7f40534

Please sign in to comment.