0.0.0 ā€¢ Published 1 year ago

worker-apollo-server v0.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

šŸ‘· worker-apollo-server Quick start

A template for kick starting a Cloudflare worker project to deploy Apollo Server v4 to Cloudflare Workers.

index.ts is the content of the Workers script.

const server = new ApolloServer<ContextValue>({
  typeDefs,
  resolvers,
  introspection: true,
  plugins: [
    ApolloServerPluginLandingPageLocalDefault({ footer: false }),
    // ApolloServerPluginLandingPageProductionDefault({
    //   graphRef: 'my-graph-id@my-graph-variant',
    //   footer: false,
    // })
  ],
});

Example

Live: https://worker-apollo-server.webee-asia.workers.dev

query {
  example
  pokemon(id: 1) {
    id
    name
  }
}

Response:

{
  "data": {
    "example": "Hello world!",
    "pokemon": {
      "id": "1",
      "name": "bulbasaur"
    }
  }
}

Development

Click Use this template button to create your own repository.

Start your dev server with following commands:

yarn install
yarn dev

Open up http://0.0.0.0:8787 and you should be ready to go!

This template uses graphql-codegen to auto-generate Typescript types from schema.graphql. The types are generated to file src/@types/schema.generated.ts. So you can import Resolver Types from there.

Deployment

If you don't already have an account, then create a cloudflare account here and after verifying your email address with Cloudflare, go to your dashboard and set up your free custom Cloudflare Workers subdomain.

Once that's done, you should be able to deploy your app:

yarn deploy
yarn run v1.22.19
$ wrangler publish
 ā›…ļø wrangler 2.1.9
-------------------
Running custom build: npm run generate && npm run build

> generate
> graphql-codegen --config codegen.yml

āœ” Parse Configuration
āœ” Generate outputs

> build
> NODE_ENV=production node worker.build.js

Your worker has access to the following bindings:
- KV Namespaces:
  - GRAPHQL_CACHE: dbdc624f2c684f1bb88fa38ab249a13e
- Vars:
  - GRAPHQL_BASE_ENDPOINT: "/"
  - GRAPHQL_KV_CACHE: "true"
Total Upload: 1520.16 KiB / gzip: 285.68 KiB
Uploaded worker-apollo-server (2.71 sec)
Published worker-apollo-server (0.28 sec)
  https://worker-apollo-server.webee-asia.workers.dev
Done in 7.29s.