Apollo: Required Shape of Server Response? - apollostack

I’ve just gotten my first relational database schema/query/resolver working in apollo/postgres/sequelize, up to the point where it’s time for the resolver to return data to the client. Evidently I don’t have the data in the correct shape yet, as it comes up null on the client.
QUERY
const CREATE_APPT_MUTATION = gql`
mutation createAPPT ($originatingUserID: String!, $apptWithUserID: String!, $apptDateTime: String!, $apptNotes: String!, $apptTitle: String!){
createAPPT(originatingUserID: $originatingUserID, apptWithUserID: $apptWithUserID, apptDateTime: $apptDateTime, apptNotes: $apptNotes, apptTitle: $apptTitle){
originatingUserID
apptWithUserID
apptDateTime
apptNotes
apptTitle
}
}
`;
CURRENT SHAPE OF RESPONSE FROM RESOLVER
Via console.log running on the server, before being sent to client:
{ data:
{ __typename: 'Mutation',
createAPPT:
{ id: '76',
originatingUserID: 'DsmkoaYPeAumREsqC',
apptWithUserID: '9W95z8A7Y6i34buk7',
apptDateTime: '2016-12-24T02:48:50.000Z',
apptTitle: 'Appointment with Benedict Sama',
apptNotes: 'asdf',
createdAt: Fri Dec 23 2016 10:49:12 GMT-0800 (PST),
updatedAt: Fri Dec 23 2016 10:49:12 GMT-0800 (PST),
UserData: [Object],
__typename: 'Appts'
}
}
}
HOW IT LOOKS IN CHROME DEV TOOLS WHEN IT COMES BACK TO THE CLIENT
mutationResult: Object
data: Object
createAPPT: Object
__typename: "Appts"
apptDateTime: null
apptNotes: null
apptTitle: null
apptWithUserID: null
originatingUserID: null
__proto__: Object
__proto__: Object
__proto__: Object
FINAL THEN BLOCK IN RESOLVER
.then(apptWithJoinedData => {
//package up the results in the way that the client is expecting
const apptDataValues = apptWithJoinedData[0].dataValues;
apptDataValues.__typename = "Appts";
var serverResponse = {};
serverResponse.data = {};
serverResponse.data.__typename = 'Mutation';
serverResponse.data.createAPPT = apptDataValues;
// publish subscription notification
debugger;
console.log(serverResponse);
pubsub.publish('APPTAdded', serverResponse);
return serverResponse;
})
Can someone point me in the direction of seeing what's wrong with the shape of the server response?

I got all the data, except for the array of UserData, to the server by changing the last then block to:
.then(apptWithJoinedData => {
// publish subscription notification
debugger;
console.log('createAPPT cp#2');
console.log(apptWithJoinedData);
pubsub.publish('APPTAdded', apptWithJoinedData);
return apptWithJoinedData;
})
I still have to get that UserData to the server, but that's a topic for another thread.

Related

Neo4j GraphQL how to check if node exists before connecting to it

B"H
I've seen this question and I want to do the same thing with the GraphQL driver for Neo4j. I think the answer lies somewhere in combining AND and OR operators found here, of some sort, but not sure how.
To illustrate the question, say I have this type:
type Comment {
id: ID #id
timeAdded: DateTime! #timestamp(
operations: [CREATE]
)
writer: User! #relationship(
type: "WRITTEN_BY",
direction: IN
)
content: String
}
type User {
id: ID #id
name: String
commentsWritten: [Comment!]! #relationship(
type: "WRITTEN_BY",
direction: OUT
)
}
type Mutation {
addComment(authorID: String)
}
and this resolver to add a new comment (where "Comment" is a reference to the OGM model of the Comment type):
addComment: async (
src,
args,
ctx
) => {
var authorID = args/*edit*/.authorID //let's say this is passed
//from request headers / JWT token or something
var adCom = await Comment.create({
input: [
{
content: args.content,
writer: {
connect: {
where: {
node: {
users: {
id: authorID
}
}
}
}
}
}
]
})
return adCom;
}
So it attempts to connect to that user with that ID. but if there is no user with that ID, I don't want to create anything [not only do I not want to connect it].
I could run another query to find a User with that ID, and test if it went through, but was wondering if it's possible to do everything in one call

How to debug Flutter Amplify Graphql requests?

I'm trying to return a list of my model sorted by date. But I run into a vague error.
Here is my graphql.schema
type Vote #model(timestamps: { createdAt: "created_at", updatedAt: "updated_at" }) {
id: ID!
name: String!
date: AWSTimestamp #index(name: "date-index", sortKeyFields: ["name"], queryField: "getVotesByDate")
}
Here is the request:
Future<List<Vote?>> recentVotes() async {
const getRecentVotes = "getRecentVotes";
String document = """
query getRecentVotes {
listVotes(limit: 5) {
items {
date
id
name
}
nextToken
}
}
""";
try {
final request = GraphQLRequest(
document: document,
modelType: Vote.classType,
decodePath: getRecentVotes,
);
final response = await Amplify.API.query(request: request).response;
List<Vote?>? votes = response.data?.items;
if (votes == null) {
return [];
}
_voteLength = votes.length;
return votes;
} catch (err) {
debugPrint(err.toString());
}
return [];
}
The error I get back is pretty terrible:
flutter: ApiException(message: The HTTP response status code is [400]., recoverySuggestion: The metadata associated with the response is contained in the HTTPURLResponse.
flutter: For more information on HTTP status codes, take a look at
flutter: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes, underlyingException: null)
As best I can tell, it's just a standard bad request error but with no message of what makes it bad. The graphql syntax is valid and tested.
*Edit
I've managed to restart the app which provided more details about the error. With the same code, I now get
flutter: type 'Null' is not a subtype of type 'String'
My first thought after this is that there's some returned field that is null that cannot be. I've checked the name and the id of all the records in the database and they are not null so I'm not sure how that's possible.

Twilio Studio - How To Access Array Items From JSON Output of Function

I'm calling the function below in Twilio Studio from a run function widget, then saving the EmployeeInfo to a variable setting the EmployeeInfo variable to value {{widgets.function_1.parsed.EmployeeInfo}} . When I put the code {{flow.variables.EmployeeInfo}} into a say/play widget it will say all of the data elements but I'm having trouble accessing a single value (like saying a single Name). I tried every conceivable syntax I could think of such as {{flow.variables.EmployeeInfo[0].Name}} and {{flow.variables.EmployeeInfo.Name[0]}} but nothing will play. Do I have some syntax error somewhere, or maybe are you not able to store arrays in variables?
exports.handler = function(context, event, callback) {
let results = {
LastName: "WAYNE",
FirstName: "BRUCE",
EmployeeInfo: [{ ID: 1, Name:"test" }, { ID: 2, Name:"tested" }, { ID: 3, Name:"testing" }]
};
return callback(null, results);
};
I am just learning twilio studio flows as well and found that the systax is "{{widgets..parsed.EmployeeInfo[0].Name}}"

Apollo server, GraphQL and Sequelize - how to put raw data into a GraphQL Schema Response

After scouring the internet for a specific example, I am throwing in the towel and asking for some help.
I am using Apollo server, GraphQL and Sequelize, and I am calling stored procedure that returns a record set created from two different tables. I am getting the data back, but I cannot figure out how to put the result into a GraphQL schema response.
Here is the code in my resolver:
async functionName(_, {input}, {user = null}) {
if (!user) {
throw new AuthenticationError('You must login to use this function');
}
const {record_id} = input;
const result = await DBService.query(
'Call sp_JoinTwoTables_Select(:id)',
{
model: FooModel,
mapToModel: true,
raw: true,
replacements: {id: record_id},
type: QueryTypes.SELECT
}
);
console.log('functionName.result');
console.log(result); // Getting results
return result;
}
Here is the code in my schema:
const {gql} = require('apollo-server-express');
module.exports = gql`
type Foo {
id: Int!
foo_name: String!
date_created: String!
date_modified: String!
}
extend type Mutation {
functionName(input: fooInput!): fooResponse!
}
input fooInput {
id: Int!
}
type fooResponse {
tree: [fooSchemaForBothTables!]
}
type fooSchemaForBothTables {
id: Int!
foo_name: String!
column_from_second_table: Int!
}
`;
Since there is no table in the database, I created a simple object. When that failed I tried a sequelized model object, but that also is failing. Here is this code:
module.exports = {FooModel: {
id: 0,
fooName: '',
column_from_second_table: 0
}};
The output I am getting is (not a 2d array as I thought):
Executing (default): Call sp_CommunityHierarchy_Select(9)
selectHierarchyTree.result
[
{
'0': {
community_id: 1,
community_name: 'Cars',
level_from_apex: null,
parent_id: null
},
'1': {
community_id: 8,
community_name: 'Chevy',
level_from_apex: 2,
parent_id: 1
},
'2': {
community_id: 9,
community_name: 'Suburban',
level_from_apex: 3,
parent_id: 8
},
meta: [ [ColumnDef], [ColumnDef], [ColumnDef], [ColumnDef] ]
},
{ affectedRows: 6, insertId: 0, warningStatus: 0 }
]
Your 'raw' DB result:
is an array;
1st element is an object with records/items encoded as index-named properties;
Your required mutation (why not a query type!?) response should tree: [fooSchemaForBothTables!] - object with tree named property (really required additional nesting level?) with an array of fooSchemaForBothTables-shaped objects as values:
{
tree: [
{
id: 1,
foo_name: 'Cars`,
column_from_second_table: 'whatever`,
},
{
id: 2,
foo_name: 'Chevy`,
column_from_second_table: 'whatever`,
}
]
}
Your job is to convert DB response into the required mutation result shape.
Hint: You can hardcode this DB result (some input const) in a side project (codesandbox) and write some conversion fn. When ready use it in this resolver.
You can also search for some more reliable sequelize (leave graphql alone for a moment) tutorials with 'more working' model mapping.
Next step?
If it is a tree then why not return this as a tree structure - nested nodes/types?

Why does "run seedDb" fail when I add in relations between nodes moving from graphql to neo4j

I got the demo example from grand-stack and was able to start up graphql, start up the Neo4J sandbox and populate the test database using
npm run seedDb
However, when I try to write my own data entries to populate into a neo4j database, I cannot get the relation between nodes to work at all. The error message is the most non-useful message (and I believe it is from the apollo client, and is a status code 400 error). I simplified the code to the most simplest case to make it work, and it still does not. Here is the schema.graphql file:
type Patient {
id: ID!
name: String
reviews: [Review] #relation(name:"WROTE", direction:"OUT")
}
type Review {
id: ID!
stars: Int
text: String
date: Date
user: Patient #relation(name: "WROTE", direction: "IN")
}
and here is the seed-mutation.js file:
export default /* GraphQL */ `
mutation {
p1: CreatePatient(
id: "p1",
name: "John Doe 1"
) {
id
name
}
r1: CreateReview(id: "r1", stars: 4, text: "Great IPA selection!", date: { formatted: "2016-01-03"}) {
id
}
ar1: AddUserReviews(from: { id: "p1"}, to: { id: "r1" }) { from {id}}
}
`;
When I do "npm run seedDb", this yields the error message:
{ Error: Network error: Response not successful: Received status code 400
at new ApolloError (/Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/apollo-client/bundle.esm.js:60:28)
at Object.error (/Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/apollo-client/bundle.esm.js:1032:48)
at notifySubscription (/Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/zen-observable/lib/Observable.js:134:18)
at onNotify (/Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/zen-observable/lib/Observable.js:165:3)
at SubscriptionObserver.error (/Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/zen-observable/lib/Observable.js:224:7)
at /Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/apollo-link-http/src/httpLink.ts:184:20
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
graphQLErrors: [],
networkError:
{ ServerError: Response not successful: Received status code 400
at Object.exports.throwServerError (/Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/apollo-link-http-common/src/index.ts:114:17)
at /Users/xxxx/Downloads/grand-stack-starter-master/api/node_modules/apollo-link-http-common/src/index.ts:145:11
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
name: 'ServerError',
response:
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: [Object],
[Symbol(Response internals)]: [Object] },
statusCode: 400,
result: { errors: [Array] } },
message: 'Network error: Response not successful: Received status code 400',
extraInfo: undefined }
I started with multiple complex codes and this is pretty much the most stripped down version. When I run the seedDB command after the seed-mutation.js file was modified to:
export default /* GraphQL */ `
mutation {
p1: CreatePatient(
id: "p1",
name: "John Doe 1"
) {
id
name
}
r1: CreateReview(id: "r1", stars: 4, text: "Great IPA selection!", date: { formatted: "2016-01-03"}) {
id
}
}
`;
the database gets populated, however the two nodes are not connected to each other, as expected (basically, this is the code with the AddUserReviews removed). How do I build the relation between the nodes through using graphql and seedDb? What am I missing?
You can use GraphQL Playground to inspect the GraphQL API (in the "Docs" tab):
to ensure the mutations you are calling have the correct name and arguments. From inspecting the schema, it looks like instead of AddUserReviews, you want AddPatientReviews?

Resources