Reusable function to query Replit's GraphQL API. Written to consume @apollo/client with a patched node-fetch used by Replit.
It is recommended you add babel-plugin-import-graphql
to your Babel configuration to import .graphql
files. Otherwise you may use graphql-tag
to the same effect.
For information on how to use the ApolloClient, visit its core API docs. Note, this package uses the core ApolloClient, not the React view layer.
import { GraphQL } from '@rayhanadev/replit-gql';
import CurrentUser from './queries/CurrentUser.graphql';
// You can use a Replit Token
// (connect.sid) for authenticated
// queries.
const { REPLIT_TOKEN } = process.env;
const client = GraphQL(REPLIT_TOKEN);
const { data } = await client.query({
query: CurrentUser,
});
console.log(data);
Output:
{
"currentUser": {
"id": 4045907
}
}