Skip to content

Commit e7feb81

Browse files
committed
added apollo-client 2 examples with react-apollo
1 parent defbca2 commit e7feb81

File tree

10 files changed

+4536
-0
lines changed

10 files changed

+4536
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
schema: schema.graphql
2+
documents: query.graphql
3+
generates:
4+
./typed-document-nodes.ts:
5+
plugins:
6+
- typescript
7+
- typescript-operations
8+
- typed-document-node
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable */
2+
import * as React from 'react';
3+
import { RatesDocument } from './typed-document-nodes';
4+
import { Query } from 'react-apollo';
5+
6+
const MyComponent: React.FC = () => {
7+
return (
8+
<div>
9+
<Query query={RatesDocument}>
10+
{
11+
({ data }) => {
12+
return data?.rates[0].rate;
13+
}
14+
}
15+
</Query>
16+
</div>
17+
)
18+
}

examples/apollo-client-2/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable */
2+
import { RatesDocument } from './typed-document-nodes';
3+
import { ApolloClient } from 'apollo-client';
4+
import { InMemoryCache } from 'apollo-cache-inmemory'
5+
6+
const client = new ApolloClient({
7+
cache: new InMemoryCache(),
8+
});
9+
10+
async function main() {
11+
// We now have types support and auto complete for the
12+
// result type, just by passing `RatesDocument` as `query` to apollo client.
13+
const result = await client.query({
14+
query: RatesDocument,
15+
variables: {
16+
currency: "USD",
17+
},
18+
});
19+
20+
const rates = result.data.rates;
21+
const currency = rates[0].rate;
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "apollo-client-example",
3+
"private": true,
4+
"version": "3.0.0",
5+
"scripts": {
6+
"postinstall": "patch-typed-document-node && yarn generate",
7+
"generate": "graphql-codegen",
8+
"precompile": "yarn generate",
9+
"compile": "tsc"
10+
},
11+
"dependencies": {
12+
"@graphql-codegen/cli": "1.17.8",
13+
"@graphql-codegen/typed-document-node": "1.17.9",
14+
"@graphql-codegen/typescript": "1.17.8",
15+
"@graphql-codegen/typescript-operations": "1.17.8",
16+
"@graphql-typed-document-node/core": "^3.1.0",
17+
"@graphql-typed-document-node/patch-cli": "^3.0.3",
18+
"@types/node": "14.0.27",
19+
"@types/react": "^16.9.46",
20+
"apollo-cache": "^1.3.5",
21+
"apollo-cache-inmemory": "^1.6.6",
22+
"apollo-cache-persist": "^0.1.1",
23+
"apollo-client": "^2.6.10",
24+
"apollo-link": "^1.2.14",
25+
"apollo-link-schema": "^1.2.5",
26+
"graphql": "15.3.0",
27+
"react": "^16.13.1",
28+
"react-apollo": "^3.1.5",
29+
"typescript": "3.9.7"
30+
}
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
query rates($currency: String!) {
2+
rates(currency: $currency) {
3+
currency
4+
rate
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"infiniteLoopProtection": true,
3+
"hardReloadOnChange": false,
4+
"template": "node"
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type ExchangeRate {
2+
currency: String
3+
rate: String
4+
name: String
5+
}
6+
7+
type Query {
8+
rates(currency: String!): [ExchangeRate]
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"compilerOptions": {
3+
"incremental": true,
4+
"baseUrl": ".",
5+
"jsx": "react",
6+
"outDir": "dist",
7+
"rootDir": "./",
8+
"esModuleInterop": true,
9+
"allowSyntheticDefaultImports": true,
10+
"importHelpers": true,
11+
"experimentalDecorators": true,
12+
"module": "esnext",
13+
"target": "es2018",
14+
"lib": ["es6", "esnext", "es2015", "dom"],
15+
"suppressImplicitAnyIndexErrors": true,
16+
"moduleResolution": "node",
17+
"emitDecoratorMetadata": true,
18+
"sourceMap": true,
19+
"declaration": true,
20+
"noImplicitThis": true,
21+
"alwaysStrict": true,
22+
"noImplicitReturns": true,
23+
"noUnusedLocals": false,
24+
"resolveJsonModule": true
25+
},
26+
"include": ["./"],
27+
"exclude": [
28+
"**/dist",
29+
"**/temp"
30+
]
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
2+
export type Maybe<T> = T | null;
3+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
4+
/** All built-in and custom scalars, mapped to their actual values */
5+
export type Scalars = {
6+
ID: string;
7+
String: string;
8+
Boolean: boolean;
9+
Int: number;
10+
Float: number;
11+
};
12+
13+
export type ExchangeRate = {
14+
__typename?: 'ExchangeRate';
15+
currency?: Maybe<Scalars['String']>;
16+
rate?: Maybe<Scalars['String']>;
17+
name?: Maybe<Scalars['String']>;
18+
};
19+
20+
export type Query = {
21+
__typename?: 'Query';
22+
rates?: Maybe<Array<Maybe<ExchangeRate>>>;
23+
};
24+
25+
26+
export type QueryRatesArgs = {
27+
currency: Scalars['String'];
28+
};
29+
30+
export type RatesQueryVariables = Exact<{
31+
currency: Scalars['String'];
32+
}>;
33+
34+
35+
export type RatesQuery = (
36+
{ __typename?: 'Query' }
37+
& { rates?: Maybe<Array<Maybe<(
38+
{ __typename?: 'ExchangeRate' }
39+
& Pick<ExchangeRate, 'currency' | 'rate'>
40+
)>>> }
41+
);
42+
43+
44+
export const RatesDocument: DocumentNode<RatesQuery, RatesQueryVariables> = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"rates"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"currency"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},"directives":[]}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"rates"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"currency"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currency"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"currency"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"rate"},"arguments":[],"directives":[]}]}}]}}]};

0 commit comments

Comments
 (0)