Nested queries with graphql schema language - javascript

I have the following in my buildSchema:
type User {
id: ID
firstname: String
age : Int
company : Company
}
type Company {
id: ID
name: String
description : String
}
type RootQuery {
user(id: ID): User
}
When making this request:
user(id:"1"){
firstname,
company{
id,
name
}
}
company is returning a null value:
{
"data": {
"user": {
"firstname": "Jhoni",
"company": null
}
}
}
How can I get the company value?

The problem here will be to do with what data is coming into your query. The execution itself is successful, but you are not getting the company data for some reason.
To debug this, I would look at what data is actually returned in your resolver for the user query by the request. It could be the reference ID parameter is not linking to any results in where you store company details.
I guess one other possibility is that you may have may not pass back the company data into a parameter named 'company', again, looking at the object that is returned to the resolver for 'user' before it is returned by the function should give you an idea of what's not matching up.

Related

Prisma assigning types that I did not delcare

I have a many to many relationship between Post and Upload and prisma is claiming upload.posts is of type never. Due to this I cannot query a relationship I need. I am not sure why Prisma is assigning type never to this. See the below findFirst statement I am trying to use to connect a upload to a post.
await prisma.upload.findFirst({
where: {
id: upload.id,
},
select: {
type: true,
},
// prisma claims posts is Type never
posts: {
connect: {
id: post.id
}
}
});
Schema for relationships
model Upload {
id Int #id #default(autoincrement())
posts Post[]
}
model Post {
id Int #id #default(autoincrement())
uploads Upload[]
}
Take a look at the Prisma documentation to see how you can use findFirst:
https://www.prisma.io/docs/concepts/components/prisma-client/crud#get-the-first-record-that-matches-a-specific-criteria
You cannot pass posts as a top-level argument to findFirst. You can't make changes to data as you are querying for it, if that is what you are trying to do.
await prisma.upload.findFirst({
where: {
id: upload.id,
},
// REMOVED: "type", your model does not have a "type" field
select: {
id: true,
}
// REMOVED: "posts"
});

DynamoDB search object with queries filter

{
"INFO": {
"email": "test#example.com",
"password": "123"
},
"PK": "3a95eab0-57de-4e15-90ea-004082e53384",
"SK": "user"
}
Above is my dataset in dynamoDB. I am building login api with expressjs with dynamodb. I am able to scan and update data with PK & SK keys but i want to query inside my INFO set.
I am trying like this:
var params = {
TableName: "table",
FilterExpression: "contains (INFO, :sendToVal)",
ExpressionAttributeValues: {
":sendToVal": {
email: "test#example.com",
password: "123",
},
},
};
But its returning:
{ error: 'Error retrieving Event' }
{ error: 'Event not found' }
Anyone help guide me, how can i retrive the set.
The DynamoDB documentation explains that the contains() function in an expression only works for strings or sets. This isn't completely accurate - it also works for lists. But in any case, it doesn't work for maps, which is what your INFO is, so the comparison doesn't match anything.
If you intended for INFO to be a list, not a map, please make it so. Otherwise, if you really intended for it to be a map, and you wanted to test whether { email: "test#example.com", password: "123" } is in that map, then what you really need to check is whether the email and password entries in this map is equal to the desired value. So the filter condition can be something like INFO.email = :email AND INFO.password = :password. Or something like this (I'm not sure I understannd what your intention was here).

How to get the type names of the query in graphql

Hi I want to get the type names of the query in graphql. It should not have scalar and enumerated types.
schema {
query: Query
}
type LearningResource{
id: ID
name: String
type: String
children: [LearningResource]
}
type Query {
fetchLearningResource: LearningResource
}
When I run fetchLearningResource query then I want to get the type of it like here LearningResource. Any way to do this in GraphQL ?
You should find the Type of the Query as a string in the Response, search for __typename.
E.g.
response =>
{
fetchLearningResource: {
id: <id>
name: "name"
type: "type"
children: [...]
__typename: "LearningResource"
}
}
EDIT:
If you want to fetch the query definition before you query. You could get the introspection query from the server. Here are some links:
introspection query error
retrieve whole schema question

How to query for dynamic amount of aliases?

Assume I have a GraphQL Schema like this:
Schema {
user($id: ID) {
id: ID
name: String
}
}
I know how to query for one user by his ID, and I could use aliases to query for several users like so:
GetSomeMore {
john: user(id: "123") {
name
}
mary: user(id: "321") {
name
}
}
However, assume I just have a list of user IDs, and would like to query for the names of each one of them. How would I write a query like that?
The approaches I can come up with is ether dynamically generating a query with aliases like user1, user2, etc., or implementing a users(ids: [ID]) edge on the server. But I feel like there should be a better solution. Is there?
(PS: my code examples might have syntax errors, I'm writing it more as pseudocode, to get my point across.)
You could implement that functionality as part of your schema, for example with a id_in: [String] argument to the users query.
At Graphcool, we use the filter argument to expose functionality like this. For example, your query could be done like this:
query {
allUsers(filter: {id_in: ["123", "321"]}) {
id
name
}
}
results in
{
"data": {
"allUsers": [
{
"id": "123",
"name": "John"
},
{
"id": "321",
"name": "Mary"
}
]
}
}
To read more about the filter argument check the documentation.
I would go with your last proposition:
Schema {
users($ids: [ID]) {
id: ID
name: String
}
}
With a resolver like:
const users = async (root, { ids }) => await knex('users').whereIn('id', ids)
Note: I use graphql-js + graphql-tools + knex.js to create my schema, my types and my resolvers.

Get user from parse in Javascript

I know this question may be asked multiple times but I am not able to get the User object in its entirity. The problem is that the user object returns only the username, email and the phone number properties while I have certain fields added like first name and last name to the user object.
I'd like to know if there's a way to get the user object in its entirity
The object on parse cloud should look like
User:
{
"firstname": "firstname",
"lastname": "lastname",
"username": "username",
"email": "abc.xyz#jkl.pqr",
"phone": "0123456789",
"type": "admin/super-admin/user"
}
I use following query to get the object
var query = new Parse.Query(Parse.User);
query.equalTo("username", "username"); // Whatever be the username passed by search string
query.find({
success: function (user) {
console.log("Success:", user)
},
error: function (error) {
//Show if no user was found to match
}
});
I get the user object as follows:
user:{
createdAt:"2014-12-27T07:16:09.826Z"
email: "abc.xyz#pqr.mno"
objectId: "223tch5S6J"
phone: "0123456789"
updatedAt: "2014-12-27T07:16:09.826Z"
username: "username"
}
I do not get the other properties like type, firstname, lastname.
Note: I do not need to get the password which parse does not provide anyways so that's not the problem here.
user.attributes should hold all the user attributes saved server side

Categories