Skip to content

Commit c22d38a

Browse files
authored
feat(graphql-client): generate ts types from operations (#1027)
1 parent 99d988f commit c22d38a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1055
-375
lines changed

.prettierignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ coverage
22
lib
33
build
44
package.json
5-
packages/graphql-api/schema.json
5+
packages/graphql-api/introspection.json
66
packages/graphql-api/src/models.ts
7+
packages/graphql-client/src/graphql-operations.ts
78
website/.docusaurus
89
website/docs/api

examples/graphql-server-typescript/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ yarn start
3737
```graphql
3838
mutation CreateUser {
3939
createUser(
40-
user: {
41-
42-
password: "1234567"
43-
profile: { firstName: "John", lastName: "Doe" }
44-
}
40+
user: { email: "[email protected]", password: "1234567", firstName: "John", lastName: "Doe" }
4541
)
4642
}
4743

examples/graphql-server-typescript/src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ const start = async () => {
2727
// This option is called when a new user create an account
2828
// Inside we can apply our logic to validate the user fields
2929
validateNewUser: (user) => {
30+
if (!user.firstName) {
31+
throw new Error('First name required');
32+
}
33+
if (!user.lastName) {
34+
throw new Error('Last name required');
35+
}
36+
3037
// For example we can allow only some kind of emails
3138
if (user.email.endsWith('.xyz')) {
3239
throw new Error('Invalid email');
@@ -61,10 +68,11 @@ const start = async () => {
6168
6269
# Our custom fields to add to the user
6370
extend input CreateUserInput {
64-
profile: CreateUserProfileInput!
71+
firstName: String!
72+
lastName: String!
6573
}
6674
67-
input CreateUserProfileInput {
75+
extend type User {
6876
firstName: String!
6977
lastName: String!
7078
}

examples/react-graphql-typescript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"apollo-link": "1.2.14",
3737
"apollo-link-http": "1.5.17",
3838
"graphql": "14.6.0",
39+
"graphql-tag": "2.10.4",
3940
"qrcode.react": "1.0.0",
4041
"react": "16.13.1",
4142
"react-apollo": "2.5.8",

examples/react-graphql-typescript/src/Home.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const GET_USER_QUERY = gql`
1515
address
1616
verified
1717
}
18+
firstName
19+
lastName
1820
}
1921
}
2022
`;
@@ -44,7 +46,8 @@ const Home = ({ history }: RouteComponentProps<{}>) => {
4446
<Typography gutterBottom>Hello {data.getUser.username}</Typography>
4547
<Typography gutterBottom>You are logged in</Typography>
4648
<Typography gutterBottom>Email: {data.getUser.emails[0].address}</Typography>
47-
<Typography gutterBottom>You username is {data.getUser.username}</Typography>
49+
<Typography gutterBottom>First name: {data.getUser.firstName}</Typography>
50+
<Typography gutterBottom>Last name: {data.getUser.lastName}</Typography>
4851
<Typography gutterBottom>
4952
You email is {data.getUser.emails[0].verified ? 'verified' : 'unverified'}
5053
</Typography>

examples/react-graphql-typescript/src/Signup.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ const Signup = ({ history }: RouteComponentProps<{}>) => {
4141
await accountsGraphQL.createUser({
4242
email: user.email,
4343
password: user.password,
44-
profile: {
45-
firstName: user.firstName,
46-
lastName: user.lastName,
47-
},
44+
firstName: user.firstName,
45+
lastName: user.lastName,
4846
});
4947
history.push('/login');
5048
} catch (err) {

examples/react-graphql-typescript/src/utils/accounts.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ApolloClient } from 'apollo-client';
66
import { HttpLink } from 'apollo-link-http';
77
import { from } from 'apollo-link';
88
import { InMemoryCache } from 'apollo-cache-inmemory';
9+
import gql from 'graphql-tag';
910

1011
// This auth link will inject the token in the headers on every request you make using apollo client
1112
const authLink = accountsLink(() => accountsClient);
@@ -19,7 +20,20 @@ const apolloClient = new ApolloClient({
1920
cache: new InMemoryCache(),
2021
});
2122

22-
const accountsGraphQL = new GraphQLClient({ graphQLClient: apolloClient });
23+
const accountsGraphQL = new GraphQLClient({
24+
graphQLClient: apolloClient,
25+
userFieldsFragment: gql`
26+
fragment userFields on User {
27+
id
28+
emails {
29+
address
30+
verified
31+
}
32+
firstName
33+
lastName
34+
}
35+
`,
36+
});
2337
const accountsClient = new AccountsClient({}, accountsGraphQL);
2438
const accountsPassword = new AccountsClientPassword(accountsClient);
2539

packages/boost/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@accounts/types": "^0.28.0",
3434
"@graphql-modules/core": "0.7.17",
3535
"apollo-server": "^2.9.3",
36-
"graphql": "^14.5.4",
36+
"graphql": "14.6.0",
3737
"graphql-tools": "^5.0.0",
3838
"jsonwebtoken": "^8.5.1",
3939
"lodash": "^4.17.15",

packages/graphql-api/codegen.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ overwrite: true
22
schema: ./src/schema.ts
33
require: ts-node/register/transpile-only
44
generates:
5+
introspection.json:
6+
plugins:
7+
- introspection
8+
config:
9+
minify: true
510
./src/models.ts:
611
config:
712
noNamespaces: true

packages/graphql-api/introspection.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)