Skip to content

Instantly share code, notes, and snippets.

@gabrielmlinassi
Last active August 5, 2022 02:47
Show Gist options
  • Save gabrielmlinassi/69255d3e28ff5a1ebcb265294e6b9856 to your computer and use it in GitHub Desktop.
Save gabrielmlinassi/69255d3e28ff5a1ebcb265294e6b9856 to your computer and use it in GitHub Desktop.
GraphQL with Vanilla JavaScript
const api = axios.create({
baseURL: "xxx/graphql",
method: "POST",
headers: { "Content-Type": "application/json" },
});
const CategoriesQuery = (first: number, after: string) =>
JSON.stringify({
query: /* GraphQL */ `
query Categories($first: Int!, $after: String!) {
categories(first: $first, after: $after) {
nodes {
id
slug
name
description
}
pageInfo {
endCursor
hasNextPage
}
}
}
`,
variables: {
first,
after,
},
});
const response = await api({ data: CategoriesQuery(2, "") });
const result = response.data;
console.log(result);
@gabrielmlinassi
Copy link
Author

Auto formatts the GraphQL query with the /* GraphQL */ tag function (ES6) from Prettier.

@gabrielmlinassi
Copy link
Author

Not needed to use gql or graphql as dependency. Not needed to use libs like Apollo, URQL, etc. Sometimes they're just overkill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment