Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 #21

Merged
merged 15 commits into from
Nov 9, 2023
Merged

V2 #21

merged 15 commits into from
Nov 9, 2023

Conversation

talentlessguy
Copy link
Member

@talentlessguy talentlessguy commented Nov 8, 2023

A re-write of gql to fully comply with GraphQL-over-HTTP spec, based on graphql-http.

Breaking Changes

  • runHttpQuery is removed. Query handling logic is now handled fully by graphql-http.
  • GQLParams is removed. Use graphql-http Request type instead.
  • request type text/plain is no longer a valid. Use application/json instead

New Features

  • All of the configuration for GraphQL-over-HTTP (for example validationRules) is now available for gql.
  • New GraphQLHTTP function argument - reqCtx. Use it to modify the request context for GraphQL Request object.

Example:

import { GraphQLHTTP } from 'https://deno.land/x/gql@2.0.0/mod.ts'
import { makeExecutableSchema } from 'npm:@graphql-tools/schema@10.0.0'
import { gql } from 'https://deno.land/x/graphql_tag@0.1.2/mod.ts'
import type { Request as GQLRequest } from 'npm:graphql-http@1.22.0'

const typeDefs = gql`
  type Query {
    hello: String
  }
`

type ReqContext = {
  request: Request
  isRequestContext: boolean
}

type Context = {
  request: Request
  originalReq: GQLRequest<Request, ReqContext>
}

const resolvers = {
  Query: {
    hello: (_root: unknown, _args: unknown, ctx: Context) => {
      return `Hello from request context: ${ctx.originalReq.context.isRequestContext}`
    },
  },
}

const schema = makeExecutableSchema({ resolvers, typeDefs })

Deno.serve({
  port: 3000,
  onListen({ hostname, port }) {
    console.log(`☁  Started on http://${hostname}:${port}`)
  },
}, async (req) => {
  const { pathname } = new URL(req.url)
  return pathname === '/graphql'
    ? await GraphQLHTTP<Request, Context, ReqContext>({
      schema,
      graphiql: true,
      context: (request) => ({ request: req, originalReq: request }),
    }, () => ({ request: req, isRequestContext: true }))(req)
    : new Response('Not Found', { status: 404 })
})

Misc

  • Added GraphQL-over-HTTP compliance tests
  • Fixed unit tests
  • Bumped GraphQL from 16.6 to 16.8.1

@talentlessguy talentlessguy merged commit f308841 into master Nov 9, 2023
2 checks passed
@talentlessguy talentlessguy deleted the v2 branch November 9, 2023 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant