I have 2 graphql services, each serving a subset of data with the same schema.
Service1 has a list of Users with ID 1-10,000
Service2 has a list of Users with ID 10,001-20,000
My User Type looks like
type User {
id: ID!
name: String!
}
Each service has a graphql query to resolve a user.
type Query {
fetchUser(id: ID!): User
fetchUsers: [User!]
}
Is there a way that apollo can query both services and merge the results,
So I dont have to query each service individually?
Related
I need to fetch a schema from graphQL API endpoint.
So the result must be a string like:
type User {
id: ID!
name: String!
}
type Home {
user: User!
address: String
country: String!
}
type Query {
MyUsers: User!
}
Is it possible to do by using codegen?
It really depends on the GraphQL server you are using, Some GraphQL servers provide a GraphQL Explorer. The GraphQL Explorer is a page where you can manually input GraphQL queries into the page such as Graphiql
Other way is to try out graphql-cli you can do with the below command,
get-graphql-schema ENDPOINT_URL -j > schema.json
I am using Postman to send GraphQL queries to my graphQL server. This is what a particular mutation schema looks like:
extend type Mutation {
createMutation(
my_id: ID!
my_data: input_data
): some_output
input input_data {
some_key: int
file : Upload!
}
I am able to perform other mutations and query through graphQL by defining appropriate GraphQL variables in here
I am not sure how to create a json value in GraphQL Variables for "file" of type Upload
I am using gqlgen to generate boilerplate for graphql queries. How do I generate a resolver for the inner nodes?
For instance, I have a Region schema with [school] array but I also need a filter for the school array. School schema contains classes array which again can be filtered by id. The below query would return all the schools and classes. But as mentioned I also need ability to filter the Schools and Classes in the same query.
type Region {
id: ID!
name: String!
schools: [School!]!
}
type School {
id: ID!
name: String!
classes: [class!]!
}
type Query {
regions(id : ID!): [Region!]!
}
Please consider the following GraphQL schema:
type User {
id: ID!
events: [Event]
}
type Event {
id: ID!
user: User!
asset: Asset!
}
type Asset {
id: ID
price: Number!
name: String!
}
GraphQL is a fantastic framework for fetching nested objects, but I'm struggling to understand how conditional nesting is implemented.
Example:
I want to retrieve all events for a specific user where asset.price is greater than x.
Or
I want to retrieve all events for an asset that belongs to a list of users [].
Question: Is conditional nesting a concept in GraphQL and how is it implemented?
Side note: I use AWS AppSync and resolvers are fetching data from AWS DynamoDB.
You can define a filter/condition on any GraphQL query such as:
query {
users(permission: 'ADMIN') {
...
}
}
The permission param is passed to your resolver (say DynamoDb VTL template, Lambda etc) to be handled however you want - to GQL this is just another parameter.
You can carry this concept into nested field by creating an events resolver and you'd then call it like this:
query {
user(id: '123') {
name
events(minPrice: 200) {
nodes: {
id
eventName
eventDate
}
}
dob
...
}
}
In above case I am using a simple minPrice param but you could do more complex things such price ranges, even pass operators (eq, gt, ...). It's all irrelevant to GraphQL - all gets passed to the resolver.
How you implement that on backend depends on your setup. I use AppSync without Amplify and write my own VTL templates and build the DynamoDb request using the provided GQL fields.
Here is an SO post that shows how to create a date filter.
I'm using backend node server with the Apollo graphql (Server side) and on the client, I'm using Apollo Client as well.
I created a few client specific types in my client Schema for Apollo client but I'm wondering:
Should I do the same for backend Types (models)? Just to add some sanity, etc.
Let me explain it a bit detailed with an example:
Here is the client schema: (Client specific types)
import gql from 'graphql-tag';
export default gql`
type System {
showSignInModal: Boolean!
}
type Robot {
name: String!
status: String!
}
type Member {
name: String!
isLogged: Boolean!
}
type Author {
id: Int!
posts: Int!
name: String
}
input AuthorInput {
id: Int!
posts: Int!
name: String
}
`;
I have a query that fetches the user data from the server (Server specific data)
so should I describe the whole User type in my schema as well?
import gql from "graphql-tag";
export const GET_USER_SHORT_DATA = gql`
mutation getUserShortData {
me {
id,
email,
name,
profileUrl,
locale
}
}
`;
Thanks for any advice!
I assume you're using mongoose for your database operations. Your main types should be defined in your mongoose schema as GraphQL is just a query language meant for "getting only what you want". So, you should add the same types in both the places. In your mongoose code for database handling & In your GraphQL Schema for query fetch support.
For Client side, if you're using a typed language like typescript, then you should define them in the client side too so as to prevent bugs & get better IDE suggestions.