Last active
August 5, 2022 02:47
-
-
Save gabrielmlinassi/69255d3e28ff5a1ebcb265294e6b9856 to your computer and use it in GitHub Desktop.
GraphQL with Vanilla JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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
Auto formatts the GraphQL query with the /* GraphQL */ tag function (ES6) from Prettier.