Working with queries
To make queries, follow these simple steps after ensuring you have the necessary prerequisites in place: a query schema and a client set up with our GraphQL adapter.
Getting Started
import { graphqlAdapter } from "@hyper-fetch/graphql";
// Initialize Client with adapter
const client = new Client({ url: "http://localhost:3000/grahql" }).setAdapter(graphqlAdapter);
// It's ready to use!
const getUser = client.createRequest<User>()({
endpoint: gql`
query GetUser {
username {
username
firstName
}
}
`,
});
const { data, status, extra, success, error } = await getUser.send();
Typescript
Depending on your schema and preferences, you can enhance the query process by incorporating types.
type User = {
username: string;
firstName: string;
};
type Variables = {
filter: string;
};
const getUser = client.createRequest<User, Variables>()({
endpoint: gql`
query GetUser {
username(filter: $filter) {
username
firstName
}
}
`,
});
const { data, status, extra, success, error } = await getUser.setData({ filter: "Some filter" }).send();