GraphQL can be somewhat verbose and annoying. Additionally requires lots of tooling to write well in an IDE. Instead let’s think of how we want to interact with our data, and then generate the graphql query automatically.
const gql = new GQLEndpoint('<https://mysite.com/gql>');
Resource → GraphQL Schema → Backend types
Nested arguments are supplemented with gql-custom schemas that inherit from base.
We might need to specify ‘… on Repository’ - not sure if this is not always the case
gql.query(
(v: { login: string }) => `query getByPinned($login: String!) {
user(login: $login) {
pinnedItems(first: 6, types: REPOSITORY) {
nodes {
... on Repository {
name
owner { login }
id
description
createdAt
stargazerCount
isFork
forkCount
isPrivate
}
}
}
}
}`,
{
user: {
pinnedItems: { nodes: [GqlRepository] },
}
},
)
gql.query(
`getByPinned($login: String!)`,
{
user: {
pinnedItems: new schema.Object(
{ nodes: [GqlRepository.join('owner')] },
`pinnedItems(first: 6, types: REPOSITORY)`
)
}
},
);
`
query {
getItems {
id
name
price
currency
}
}
`
gql.query(
`getItems`,
{ getItems: new schema.Collection([Item]) },
);
TODO: Check that mutation with user(user: $user)
rather than updateUser(user: $user)
is valid and works
export const UserResource = {
get: gql.query(
`query GetUser($id: ID!) {
user(id: $id) {
id
name
username
email
phone
website
}
}
`,
{ user: User },
),
update: gql.mutation(
`mutation updateUser($user: User!) {
user(user: $user) {
id
name
username
email
phone
website
}
}`,
{ updateUser: User },
),
};
export const UserResource = {
get: gql.query(`getUser($id: ID!)`, { user: User }),
update: gql.mutation(`updateUser($user: User!)`, { user: user }),
};
UserResource.update({ id: 'myid', username: 'bob' });
const UserResource = createResource({
schema: Todo,
methods: ['get', 'update'],
});