Typed client GraphQL using Apollo
npm install ts-graphql-clientThis client is an abstraction of Apollo client to be used with typed methods.
- Create a client
- Call mutation GraphQL
- Call query GraphQL
- Stringify a query or mutation without use client
Before installing, download and install Node.js. Node.js 14.0 or higher is required.
``bash `
npm install ts-graphql-client`
orbash `
yarn add ts-graphql-client
`typescript
import { GraphQLClient } from 'ts-graphql-client'
type User = {
id: string;
name: string;
username: string;
email: string;
phone: string;
}
const client = new GraphQLClient('https://api.graphqlplaceholder.com/')
async function main() {
const users = await client
.query<{ data: User[] }, {}>(
'users',
{},
{
data: {
id: true,
name: true,
username: true,
email: true,
phone: true,
},
}
)
.then((res) => res.toJSON().data)
console.log(users)
}
main()
`
You can test it with stackblitz : ts-graphql-client-example
#### Quick start
`typescript`
constructor(uri?: string, verbose?: boolean);
`typescript`
new GraphQLClient('http://localhost:3000/graphql');
#### With options
`typescript`
constructor(options?: ClientOptions, verbose?: boolean);
`typescript`
new GraphQLClient({
uri: 'http://localhost:3000/graphql',
fetch: fetch as any,
});
More information about available options, read apollo-boost
`typescript`
query
query
mutate
mutate
You can use queryToString` to parse your typed query without use client
To generate types, we suggest to install graphql-code-generator
Property of traveljuice