在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):atulmy/gql-query-builder开源软件地址(OpenSource Url):https://github.com/atulmy/gql-query-builder开源编程语言(OpenSource Language):TypeScript 100.0%开源软件介绍(OpenSource Introduction):GraphQL Query BuilderA simple helper function to generate GraphQL queries using plain JavaScript Objects (JSON). UsageInstall
Getting StartedYou can also import import { query, mutation, subscription } from 'gql-query-builder'
query(options: object)
mutation(options: object)
subscription(options: object) APIimport * as gql from 'gql-query-builder'
const query = gql.query(options: object, adapter?: MyCustomQueryAdapter,config?: object)
const mutation = gql.mutation(options: object, adapter?: MyCustomQueryAdapter)
const subscription = gql.subscription(options: object, adapter?: MyCustomSubscriptionAdapter) Options
config
AdapterAn optional second argument If adapter is undefined then ExamplesQuery: import * as gql from 'gql-query-builder'
const query = gql.query({
operation: 'thoughts',
fields: ['id', 'name', 'thought']
})
console.log(query)
// Output
query {
thoughts {
id,
name,
thought
}
} Query (with variables): import * as gql from 'gql-query-builder'
const query = gql.query({
operation: 'thought',
variables: { id: 1 },
fields: ['id', 'name', 'thought']
})
console.log(query)
// Output
query ($id: Int) {
thought (id: $id) {
id, name, thought
}
}
// Variables
{ "id": 1 } Query (with nested fields selection) import * as gql from 'gql-query-builder'
const query = gql({
operation: 'orders',
fields: [
'id',
'amount',
{
user: [
'id',
'name',
'email',
{
address: [
'city',
'country'
]
}
]
}
]
})
console.log(query)
// Output
query {
orders {
id,
amount,
user {
id,
name,
email,
address {
city,
country
}
}
}
} Query (with required variables): import * as gql from 'gql-query-builder'
const query = gql.query({
operation: 'userLogin',
variables: {
email: { value: '[email protected]', required: true },
password: { value: '123456', required: true }
},
fields: ['userId', 'token']
})
console.log(query)
// Output
query ($email: String!, $password: String!) {
userLogin (email: $email, password: $password) {
userId, token
}
}
// Variables
{
email: "[email protected]",
password: "123456"
} Query (with custom argument name): import * as gql from 'gql-query-builder'
const query = gql.query([{
operation: "someoperation",
fields: [{
operation: "nestedoperation",
fields: ["field1"],
variables: {
id2: {
name: "id",
type: "ID",
value: 123,
},
},
}, ],
variables: {
id1: {
name: "id",
type: "ID",
value: 456,
},
},
}, ]);
console.log(query)
// Output
query($id2: ID, $id1: ID) {
someoperation(id: $id1) {
nestedoperation(id: $id2) {
field1
}
}
}
// Variables
{
"id1": 1,
"id2": 1
} Query (with operation name): import * as gql from 'gql-query-builder'
const query = gql.query({
operation: 'userLogin',
fields: ['userId', 'token']
}, null, {
operationName: 'someoperation'
})
console.log(query)
// Output
query someoperation {
userLogin {
userId
token
}
} Query (with empty fields): import * as gql from 'gql-query-builder'
const query = gql.query([{
operation: "getFilteredUsersCount",
},
{
operation: "getAllUsersCount",
fields: []
},
operation: "getFilteredUsers",
fields: [{
count: [],
}, ],
]);
console.log(query)
// Output
query {
getFilteredUsersCount
getAllUsersCount
getFilteredUsers {
count
}
} Query (with adapter defined): For example, to inject import * as gql from 'gql-query-builder'
import MyQueryAdapter from 'where/adapters/live/MyQueryAdapter'
const query = gql.query({
operation: 'thoughts',
fields: ['id', 'name', 'thought']
}, MyQueryAdapter)
console.log(query)
// Output
query SomethingIDidInMyAdapter {
thoughts {
id,
name,
thought
}
} Take a peek at DefaultQueryAdapter to get an understanding of how to make a new adapter. Mutation: import * as gql from 'gql-query-builder'
const query = gql.mutation({
operation: 'thoughtCreate',
variables: {
name: 'Tyrion Lannister',
thought: 'I drink and I know things.'
},
fields: ['id']
})
console.log(query)
// Output
mutation ($name: String, $thought: String) {
thoughtCreate (name: $name, thought: $thought) {
id
}
}
// Variables
{
"name": "Tyrion Lannister",
"thought": "I drink and I know things."
} Mutation (with required variables): import * as gql from 'gql-query-builder'
const query = gql.mutation({
operation: 'userSignup',
variables: {
name: { value: 'Jon Doe' },
email: { value: '[email protected]', required: true },
password: { value: '123456', required: true }
},
fields: ['userId']
})
console.log(query)
// Output
mutation ($name: String, $email: String!, $password: String!) {
userSignup (name: $name, email: $email, password: $password) {
userId
}
}
// Variables
{
name: "Jon Doe",
email: "[email protected]",
password: "123456"
} Mutation (with custom types): import * as gql from 'gql-query-builder'
const query = gql.mutation({
operation: "userPhoneNumber",
variables: {
phone: {
value: { prefix: "+91", number: "9876543210" },
type: "PhoneNumber",
required: true
}
},
fields: ["id"]
})
console.log(query)
// Output
mutation ($phone: PhoneNumber!) {
userPhoneNumber (phone: $phone) {
id
}
}
// Variables
{
phone: {
prefix: "+91", number: "9876543210"
}
} AxiosExample withQuery: import axios from "axios";
import { query } from "gql-query-builder";
async function getThoughts() {
try {
const response = await axios.post(
"http://api.example.com/graphql",
query({
operation: "thoughts",
fields: ["id", "name", "thought"],
})
);
console.log(response);
} catch (error) {
console.log(error);
}
} Mutation: import axios from "axios";
import { mutation } from "gql-query-builder";
async function saveThought() {
try {
const response = await axios.post(
"http://api.example.com/graphql",
mutation({
operation: "thoughtCreate",
variables: {
name: "Tyrion Lannister",
thought: "I drink and I know things.",
},
fields: ["id"],
})
);
console.log(response);
} catch (error) {
console.log(error);
}
} Mutation (with adapter defined): For example, to inject import * as gql from 'gql-query-builder'
import MyMutationAdapter from 'where/adapters/live/MyQueryAdapter'
const query = gql.mutation({
operation: 'thoughts',
fields: ['id', 'name', 'thought']
}, MyMutationAdapter)
console.log(query)
// Output
mutation SomethingIDidInMyAdapter {
thoughts {
id,
name,
thought
}
} Take a peek at DefaultMutationAdapter to get an understanding of how to make a new adapter. Subscription: import axios from "axios";
import { subscription } from "gql-query-builder";
async function saveThought() {
try {
const response = await axios.post(
"http://api.example.com/graphql",
subscription({
operation: "thoughtCreate",
variables: {
name: "Tyrion Lannister",
thought: "I drink and I know things.",
},
fields: ["id"],
})
);
console.log(response);
} catch (error) {
console.log(error);
}
} Subscription (with adapter defined): For example, to inject import * as gql from 'gql-query-builder'
import MySubscriptionAdapter from 'where/adapters/live/MyQueryAdapter'
const query = gql.subscription({
operation: 'thoughts',
fields: ['id', 'name', 'thought']
}, MySubscriptionAdapter)
console.log(query)
// Output
subscription SomethingIDidInMyAdapter {
thoughts {
id,
name,
thought
}
} Take a peek at DefaultSubscriptionAdapter to get an understanding of how to make a new adapter. ShowcaseFollowing projects are using gql-query-builder
AuthorContributorsIf you are interested in actively maintaining / enhancing this project, get in touch.
DonateIf you liked this project, you can donate to support it < |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论