Appsync Subscriptions not working , but queries and mutations are working fine - android-emulator

I am building a simple React Native app. to test AppSync APIs. I am able to do queries, mutations ; but subscriptions don't seem to work. I am trying this out on an Android Emulator.
Here's how i am building my client and creating a subscription.
const client = new AWSAppSyncClient({
url: awsconfig.aws_appsync_graphqlEndpoint,
region: awsconfig.aws_appsync_region,
auth: {
type: AUTH_TYPE.API_KEY, // or type: awsconfig.aws_appsync_authenticationType,
apiKey: awsconfig.aws_appsync_apiKey,
}
});
subscription = client.subscribe({ query: gql(onCreateBook) }).subscribe({
next: data => {
console.log("got a book--->");
},
error: error => {
console.warn("errror getting book");
}
});
Here is my schema(relevant parts) & subscriptions gql(auto generated by codeGen)
Schema
type Book {
title: String!
description: String
}
type Mutation {
createBook(input: CreateBookInput!): Book
updateBook(input: UpdateBookInput!): Book
deleteBook(input: DeleteBookInput!): Book
}
type Query {
getBook(title: String!): Book
listBooks(filter: TableBookFilterInput, limit: Int, nextToken: String): BookConnection
}
type Subscription {
onCreateBook(title: String, description: String): Book
#aws_subscribe(mutations: ["createBook"])
onUpdateBook(title: String, description: String): Book
#aws_subscribe(mutations: ["updateBook"])
onDeleteBook(title: String, description: String): Book
#aws_subscribe(mutations: ["deleteBook"])
}
subscriptions gql
// eslint-disable
// this is an auto generated file. This will be overwritten
export const onCreateBook = `subscription OnCreateBook($title: String, $description: String) {
onCreateBook(title: $title, description: $description) {
title
description
}
}
`;
export const onUpdateBook = `subscription OnUpdateBook($title: String, $description: String) {
onUpdateBook(title: $title, description: $description) {
title
description
}
}
`;
export const onDeleteBook = `subscription OnDeleteBook($title: String, $description: String) {
onDeleteBook(title: $title, description: $description) {
title
description
}
}
`;
Note : I have verified that subscriptions are working fine on firing a mutation in AWS Console, but i cant see any errors in ReactNative app.

Related

Why does GraphQl return null?

I am trying to create an API with MongoDB, Express Js, Node and GraphQl. I have a collection called characters, with the following schema:
const CharacterSchema = Schema({
page:{
type: Number,
required: true
},
data:{
type: Array,
required: true
}
});
I have 25 objects in my database with the above schema. I have a query to query the characters, passing the page number by parameter:
type Character {
_id: ID
name: String!
status: String!
species: String!
type: String!
gender: String!
origin: String!
image: String!
episode: [String]
location: String!
created: String!
}
type Page {
page: Int!
data: [Character]!
}
type Query {
characters(page: Int!): Page!
}
And this is its resolver:
export const resolvers = {
Query: {
characters: async (_, args) => {
let data = await Character.findOne({ page: args.page });
return data;
},
},
};
This is the query Im using to fetch the data:
query($page: Int!) {
characters(page: $page) {
page
data {
name
status
species
type
gender
origin
image
episode
location
created
}
}
}
Executing the query by passing the page number, it returns perfectly the information I ask for.
Now I want to get only one character by its ID. I created a query and a type to fetch only one character by its id:
type CharacterById {
result: Character
}
type Query {
characters(page: Int!): Page!,
character(id: ID): CharacterById
}
This is its resolver:
export const resolvers = {
Query: {
//this works perfectly
characters: async (_, args) => {
let data = await Character.findOne({ page: args.page });
return data;
},
//returns obj but show me null
character: async (_, args) => {
//first method returns the object perfectly
let data = await Character.aggregate([
{ $unwind: "$data" },
{ $match: { "data._id": args.id } },
]);
return data[0].data // returns object
//second method returns the object perfectly
let data = await Character.findOne({"data._id": args.id})
let character = data.data.find(item => item._id === args.id)
return character // returns object
},
},
};
I explain the above: The query “character” is the resolver that I created to get from the database the character with the id passed by parameter.
I try it with two methods. Both of them return me perfectly the object with the id passed by parameter, but when I try to use the query:
query($characterId: ID!) {
character(id: $characterId) {
result {
name
status
species
type
gender
origin
image
episode
location
created
}
}
}
It returns me a null, when it should return me the object:
{
"data": {
"character": null
}
}
why doesn't it bring me the object?
please help me I am very stressed and frustrated that this is not working for me :(

Why mockReturnValueOnce seems not to work?

I'm trying to use Jest along Meteor, but I'm getting the following problem while trying to implement tests using Collection.find().fetch().
I'm mocking the function find and fetch, as you can see below. However, the value return by fetch is always the first one.
Shouldn't the value be different every time, since I'm using mockReturnValueOnce?
import { describe, test } from "#jest/globals";
import { Users } from "../../../collections"; // a Mongo collection
describe("#Users test suite", () => {
const mock1 = {
userId: "111aaa",
email: "user1#test.com",
};
const mock2 = {
userId: "222bbb",
email: "user1#test.com",
};
const mock3 = {
userId: "333ccc",
email: "user1#test.com",
};
test("Fetch test", () => {
Users.find.mockImplementation(() => ({
fetch: jest
.fn()
.mockReturnValueOnce(mock1)
.mockReturnValueOnce(mock2)
.mockReturnValueOnce(mock3),
}));
// getting same result in every fetch call
console.log(Users.find().fetch()); // { userId: '111aaa', email: 'user1#test.com' }
console.log(Users.find().fetch()); // { userId: '111aaa', email: 'user1#test.com' }
console.log(Users.find().fetch()); // { userId: '111aaa', email: 'user1#test.com' }
});
});
Thanks in advance!

How to query in GraphQL with Sequelize a many to many association? Receiving null error

I have many to many association, models are Decks and Tag. I am able to console.log a JSON string with both objects, but I am unsure how to set my schema and resolver to return both in one query together, and currently receiving null. I'm using GraphQL with Sequelize on an Apollo server, with PostgreSQL as the db. What's the proper method to query this relationship in GraphQL, I presume I am not returning the data properly for GraphQL to read it?
Models
Deck.belongsToMany(models.Tag, {
through: models.DeckTag,
onDelete: "CASCADE"
});
Tag.associate = models => {
Tag.belongsToMany(models.Deck, {
through: models.DeckTag,
onDelete: "CASCADE"
});
Schema for Decks
export default gql`
extend type Query {
decks(cursor: String, limit: Int): DeckConnection!
deck(id: ID, deckname: String): Deck!
decksWithTags: [Deck!]!
}
extend type Mutation {
createDeck(deckname: String!, description: String!): Deck!
deleteDeck(id: ID!): Boolean!
}
type DeckConnection {
edges: [Deck!]!
pageInfo: DeckPageInfo!
}
type DeckPageInfo {
hasNextPage: Boolean!
endCursor: String!
}
type Deck {
id: ID!
description: String!
createdAt: Date!
user: User!
cards: [Card!]
}
`;
Resolver in question
decksWithTags: async (parent, args, { models }) => {
return await models.Deck.findAll({
include: [models.Tag]
}).then(tags => {
console.log(JSON.stringify(tags)); //able to console.log correctly
});
},
Shortened sample Console.logged JSON String
[
{
"id":1,
"deckname":"50 words in Chinese",
"description":"Prepare for your immigration interview",
***
"userId":1,
"tags":[
{
"id":1,
"tagname":"Chinese",
***
"decktag":{
***
"deckId":1,
"tagId":1
}
},
{
"id":2,
***
{
"id":2,
"deckname":"English",
***
I expect to get a result in GraphQL playground that looks similar to the JSON string.

How to make GraphQL automatically insert current UTC upon mutation?

My mutation code looks like this:
Mutation: {
addPost: async (parent, args) => {
// Add new post to dbPosts
const task = fawn.Task();
task.save(
dbPost,
{
_id: new mongoose.Types.ObjectId(),
title: args.title,
content: args.content,
created: args.created,
author: {
id: args.author_id,
first_name: args.author_first_name,
last_name: args.author_last_name,
}
}
);
}
}
The schema I'm working with is defined as:
scalar DateTime
type Query {
posts: [Post],
post(id: ID!): Post,
}
type Mutation {
addPost(
title: String!,
content: String!,
created: DateTime!,
author_id: String!,
author_first_name: String!
author_last_name: String!): Post,
}
type Post {
id: ID!
title: String!,
content: String!,
author: Author!,
created: DateTime,
}
As apparent, I'm also using a custom scalar to handle date/time values. This custom scalar, DateTime resolves as:
const { GraphQLScalarType } = require('graphql/type');
const tmUTC = () => {
const tmLoc = new Date();
return tmLoc.getTime() + tmLoc.getTimezoneOffset() * 60000;
};
DateTime = new GraphQLScalarType({
name: 'DateTime',
description: 'Date/Time custom scalar type',
parseValue: () => { // runs on mutation
return tmUTC();
},
serialize: (value) => { // runs on query
return new Date(value.getTime());
},
parseLiteral: () => {
return tmUTC();
},
});
module.exports = DateTime;
Now this works fine and I'm able to insert and retrieve entries with the timestamp as expected. However, I still have to pass a dummy argument for the created field in order for the DateTime resolver to kick in:
mutation{
addPost(
title: "Ghostbusters",
content: "Lots and lots of ghosts here...",
created: "",
author_id: "5ba0c2491c9d440000ac8fc3",
author_first_name: "Bill",
author_last_name: "Murray"
){
title
content
id
created
}
}
I can even leave that field blank and the time will still get recorded. But I cannot just leave it out in my mutation call. Is there any way to achieve this? The objective here is to have GraphQL automatically execute the DateTime resolver without the user having to explicitly enter a created field in the mutation call.
in your mutation, remove the requirement for the created to be required
type Mutation {
addPost(
title: String!,
content: String!,
// created: DateTime!, changed in next line
created: DateTime, // no ! means not required
author_id: String!,
author_first_name: String!
author_last_name: String!): Post,
}
Then in your task merge in the created arg if it is not
addPost: async (parent, args) => {
// if args does not have created, make it here if it is required by task
const task = fawn.Task();
task.save(
dbPost,

Relationships GraphQL

The second week I try to link two collections in the apollo-server-express / MongoDB / Mongoose / GraphQL stack, but I do not understand how. I found a similar lesson with the REST API, what I need is called Relationships. I need this, but in GraphQL
watch video
How to add cars to the User?
I collected the test server, the code is here: https://github.com/gHashTag/test-graphql-server
Help
I have cloned your project and implemented some code and here what I changed to make relationship works. Note, I just did a basic code without validation or advance dataloader just to make sure non-complexity. Hope it can help.
src/graphql/resolvers/car-resolvers.js
import Car from '../../models/Car'
import User from '../../models/User'
export default {
getCar: (_, { _id }) => Car.findById(_id),
getCars: () => Car.find({}),
getCarsByUser: (user, {}) => Car.find({seller: user._id }), // for relationship
createCar: async (_, args) => {
// Create new car
return await Car.create(args)
}
}
src/graphql/resolvers/user-resolvers.js
import User from '../../models/User'
export default {
getUser: (_, { _id }) => User.findById(_id),
getUsers: () => User.find({}),
getUserByCar: (car, args) => User.findById(car.seller), // for relationship
createUser: (_, args) => {
return User.create(args)
}
}
src/graphql/resolvers/index.js
import UserResolvers from './user-resolvers'
import CarResolvers from './car-resolvers'
export default {
User:{
cars: CarResolvers.getCarsByUser // tricky part to link query relation ship between User and Car
},
Car:{
seller: UserResolvers.getUserByCar // tricky part to link query relation ship between User and Car
},
Query: {
getUser: UserResolvers.getUser,
getUsers: UserResolvers.getUsers,
getCar: CarResolvers.getCar,
getCars: CarResolvers.getCars
},
Mutation: {
createUser: UserResolvers.createUser,
createCar: CarResolvers.createCar,
}
}
src/graphql/schema.js
export default`
type Status {
message: String!
}
type User {
_id: ID!
firstName: String
lastName: String
email: String
cars: [Car]
}
type Car {
_id: ID
make: String
model: String
year: String
seller: User
}
type Query {
getUser(_id: ID!): User
getUsers: [User]
getCar(_id: ID!): Car
getCars: [Car]
}
type Mutation {
createUser(firstName: String, lastName: String, email: String): User
// change from _id to seller, due to base on logic _id conflict with CarId
createCar(seller: ID!, make: String, model: String, year: String): Car
}
schema {
query: Query
mutation: Mutation
}
`
src/middlewares.js
import bodyParser from 'body-parser'
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
import { makeExecutableSchema } from 'graphql-tools'
import typeDefs from '../graphql/schema'
import resolvers from '../graphql/resolvers'
import constants from './constants'
export const schema = makeExecutableSchema({
typeDefs,
resolvers
})
export default app => {
app.use('/graphiql', graphiqlExpress({
endpointURL: constants.GRAPHQL_PATH
}))
app.use(
constants.GRAPHQL_PATH,
bodyParser.json(),
graphqlExpress(req => ({
schema,
context: {
event: req.event
}
}))
)
}
try to make something like this in your car resolver
export default {
getCar: ({ _id: ownId }, { _id }) =>
Car.findById(ownId || _id);
// here is the rest of your code
You need to add a resolver for the cars field on the User type.
const resolvers = {
Query: {
getUsers: ...
getCars: ...
...
},
Mutation: {
...
},
User: {
cars: ...
}
}