Skip to content
This repository was archived by the owner on Mar 16, 2023. It is now read-only.

Run test API in browser to support codeflow #13

Merged
merged 4 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 0 additions & 78 deletions api/main.js

This file was deleted.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"lint": "eslint \"src/**/*.ts*\"",
"release": "np",
"start": "vite --open --mode ssr",
"qwik": "qwik",
"api": "node ./api/main.js"
"qwik": "qwik"
},
"main": "./lib/index.qwik.cjs",
"qwik": "./lib/index.qwik.mjs",
Expand Down Expand Up @@ -62,15 +61,13 @@
"@types/eslint": "8.4.6",
"@types/node": "latest",
"@typescript-eslint/eslint-plugin": "5.40.1",
"@typescript-eslint/parser": "5.40.0",
"@typescript-eslint/parser": "^5.40.1",
"@urql/exchange-auth": "^1.0.0",
"@urql/exchange-execute": "^2.0.0",
"@urql/exchange-graphcache": "^5.0.2",
"cookie": "^0.5.0",
"cors": "^2.8.5",
"eslint": "8.25.0",
"eslint-plugin-qwik": "latest",
"express": "^4.18.2",
"express-graphql": "^0.12.0",
"graphql": "^16.6.0",
"node-fetch": "3.2.10",
"np": "7.6.2",
Expand Down
51 changes: 51 additions & 0 deletions src/example/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { buildSchema } from 'graphql';

// Create local schema
export const schema = buildSchema(`
type Film {
id: String!
title: String!
}

input AddFilmInput {
title: String!
}

input UpdateFilmInput {
id: String!
title: String!
}

type Query {
film(id: String!): Film
}

type Mutation {
addFilm(input: AddFilmInput!): Film!
updateFilm(input: UpdateFilmInput!): Film!
}
`);

// Create local state
const data = ['Shrek', 'The Matrix', 'The Lord of the Rings', 'Something else'];

// Create root value with resolvers
export const rootValue = {
film: ({ id }: { id: string }) => {
return {
title: data[parseInt(id) % data.length],
id,
};
},
addFilm: ({ input }: { input: { title: string } }) => {
const { title } = input;
const id = data.length;
data.push(title);
return { title, id };
},
updateFilm: async ({ input }: { input: { title: string; id: string } }) => {
const { title, id } = input;
data[parseInt(id, 10)] = title;
return { title, id };
},
};
13 changes: 6 additions & 7 deletions src/example/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
createClient,
dedupExchange,
fetchExchange,
makeOperation,
} from '@urql/core';
import { createClient, dedupExchange, makeOperation } from '@urql/core';
import { authExchange } from '@urql/exchange-auth';
import { executeExchange } from '@urql/exchange-execute';
import { cacheExchange } from '@urql/exchange-graphcache';
import { qwikExchange } from '../exchange/qwik-exchange';
import { ClientFactory, UrqlAuthTokens } from '../types';
import { rootValue, schema } from './api';

export const clientFactory: ClientFactory = ({ authTokens, qwikStore }) => {
const auth = authExchange<UrqlAuthTokens>({
Expand Down Expand Up @@ -61,7 +58,9 @@ export const clientFactory: ClientFactory = ({ authTokens, qwikStore }) => {
dedupExchange,
cacheExchange(),
auth,
fetchExchange,

// Replace with fetchExchange for live requests
executeExchange({ schema, rootValue }),
],
});
};
Loading