Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #111 from michaeldgraham/master
Browse files Browse the repository at this point in the history
ID generation, scalar mutation args, tests
  • Loading branch information
johnymontana authored Sep 23, 2018
2 parents d2dca74 + 5213d45 commit c898ef5
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 105 deletions.
25 changes: 16 additions & 9 deletions src/augment.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ const possiblyAddTypeInput = (astNode, typeMap) => {
else if(getTypeDirective(astNode, "relation")) {
if(typeMap[inputName] === undefined) {
let fieldName = "";
let fieldValueType = "";
let valueType = {};
let valueTypeName = "";
let isRequired = false;
const hasSomePropertyField = astNode.fields.find(e => e.name.value !== "from" && e.name.value !== "to");
if(hasSomePropertyField) {
Expand All @@ -151,11 +152,14 @@ const possiblyAddTypeInput = (astNode, typeMap) => {
fieldName = t.name.value;
isRequired = isNonNullType(t);
if(fieldName !== "_id" && fieldName !== "to"
&& fieldName !== "from" && !getFieldDirective(t, "cypher")) {
fieldValueType = getNamedType(t).name.value;
// TODO allow custom scalars or enums?
if(isBasicScalar(fieldValueType)) {
acc.push(`${t.name.value}: ${fieldValueType}${isRequired ? '!' : ''}`);
&& fieldName !== "from" && !isListType(t)
&& !getFieldDirective(t, "cypher")) {
valueTypeName = getNamedType(t).name.value;
valueType = typeMap[valueTypeName];
if(isBasicScalar(valueTypeName)
|| isKind(valueType, "EnumTypeDefinition")
|| isKind(valueType, "ScalarTypeDefinition")) {
acc.push(`${t.name.value}: ${valueTypeName}${isRequired ? '!' : ''}`);
}
}
return acc;
Expand Down Expand Up @@ -372,7 +376,8 @@ const buildAllFieldArguments = (namePrefix, astNode, typeMap) => {
&& !isListType(t)
&& !getFieldDirective(t, "cypher")
&& (isBasicScalar(valueTypeName)
|| isKind(valueType, "EnumTypeDefinition"))) {
|| isKind(valueType, "EnumTypeDefinition")
|| isKind(valueType, "ScalarTypeDefinition"))) {
// Require if required
if(isNonNullType(t)) {
// Regardless of whether it is NonNullType,
Expand Down Expand Up @@ -455,7 +460,8 @@ const buildAllFieldArguments = (namePrefix, astNode, typeMap) => {
&& !isListType(t)
&& !getFieldDirective(t, "cypher")
&& (isBasicScalar(valueTypeName)
|| isKind(valueType, "EnumTypeDefinition"))) {
|| isKind(valueType, "EnumTypeDefinition")
|| isKind(valueType, "ScalarTypeDefinition"))) {
acc.push({
"kind": "InputValueDefinition",
"name": {
Expand Down Expand Up @@ -709,7 +715,8 @@ const createQueryArguments = (astNode, typeMap) => {

const isQueryArgumentFieldType = (type, valueType) => {
return isBasicScalar(type.name.value)
|| isKind(valueType, "EnumTypeDefinition");
|| isKind(valueType, "EnumTypeDefinition")
|| isKind(valueType, "ScalarTypeDefinition");
}

const initializeOperationTypes = (types, typeMap) => {
Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,19 @@ export function cypherMutation(
paramIndex: 1
});
params = { ...params, ...subParams };


const args = resolveInfo.schema
.getMutationType()
.getFields()[resolveInfo.fieldName].astNode.arguments;

const firstIdArg = args.find(e => getNamedType(e).type.name.value);
if(firstIdArg) {
const argName = firstIdArg.name.value;
if(params.params[argName] === undefined) {
query += `SET ${variableName}.${argName} = apoc.create.uuid() `;
}
}

query += `RETURN ${variableName} {${subQuery}} AS ${variableName}`;
} else if (isAddMutation(resolveInfo)) {
let mutationMeta, relationshipNameArg, fromTypeArg, toTypeArg;
Expand Down
153 changes: 141 additions & 12 deletions test/augmentSchemaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { printSchema } from 'graphql';
test.cb('Test augmented schema', t => {
let schema = augmentedSchema();

let expectedSchema = `enum _ActorOrdering {
let expectedSchema = `input _ActorInput {
id: ID!
}
enum _ActorOrdering {
id_asc
id_desc
name_asc
Expand All @@ -14,28 +18,125 @@ test.cb('Test augmented schema', t => {
_id_desc
}
type _AddActorMoviesPayload {
from: Actor
to: Movie
}
type _AddGenreMoviesPayload {
from: Movie
to: Genre
}
type _AddMovieActorsPayload {
from: Actor
to: Movie
}
type _AddMovieFilmedInPayload {
from: Movie
to: State
}
type _AddMovieGenresPayload {
from: Movie
to: Genre
}
type _AddMovieWatchedByPayload {
from: User
to: Movie
rating: Int
}
type _AddUserWatchedPayload {
from: User
to: Movie
rating: Int
}
input _BookInput {
genre: BookGenre!
}
enum _BookOrdering {
_id_asc
_id_desc
}
input _GenreInput {
name: String!
}
enum _GenreOrdering {
name_desc
name_asc
}
input _MovieInput {
movieId: ID!
}
enum _MovieOrdering {
title_desc
title_asc
}
type _MovieWatchedBy {
rating: Int
User(first: Int, offset: Int, orderBy: _UserOrdering): User
}
type _RemoveActorMoviesPayload {
from: Actor
to: Movie
}
type _RemoveGenreMoviesPayload {
from: Movie
to: Genre
}
type _RemoveMovieActorsPayload {
from: Actor
to: Movie
}
type _RemoveMovieFilmedInPayload {
from: Movie
to: State
}
type _RemoveMovieGenresPayload {
from: Movie
to: Genre
}
type _RemoveMovieWatchedByPayload {
from: User
to: Movie
}
type _RemoveUserWatchedPayload {
from: User
to: Movie
}
input _StateInput {
name: String!
}
enum _StateOrdering {
name_asc
name_desc
_id_asc
_id_desc
}
input _UserInput {
id: ID!
}
enum _UserOrdering {
id_asc
id_desc
Expand All @@ -45,6 +146,15 @@ enum _UserOrdering {
_id_desc
}
type _UserWatched {
rating: Int
Movie(first: Int, offset: Int, orderBy: _MovieOrdering): Movie
}
input _WatchedInput {
rating: Int
}
type Actor implements Person {
id: ID!
name: String
Expand All @@ -63,6 +173,8 @@ enum BookGenre {
Math
}
scalar DateTime
type Genre {
_id: Int
name: String
Expand All @@ -75,6 +187,7 @@ type Movie {
movieId: ID!
title: String
year: Int
released: DateTime
plot: String
poster: String
imdbRating: Float
Expand All @@ -88,30 +201,39 @@ type Movie {
scaleRating(scale: Int = 3): Float
scaleRatingFloat(scale: Float = 1.5): Float
actorMovies(first: Int, offset: Int, orderBy: _MovieOrdering): [Movie]
watchedBy: [_MovieWatchedBy]
}
type Mutation {
CreateMovie(movieId: ID, title: String, year: Int, plot: String, poster: String, imdbRating: Float, avgStars: Float): Movie
UpdateMovie(movieId: ID!, title: String, year: Int, plot: String, poster: String, imdbRating: Float, avgStars: Float): Movie
CreateMovie(movieId: ID, title: String, year: Int, released: DateTime, plot: String, poster: String, imdbRating: Float, avgStars: Float): Movie
UpdateMovie(movieId: ID!, title: String, year: Int, released: DateTime, plot: String, poster: String, imdbRating: Float, avgStars: Float): Movie
DeleteMovie(movieId: ID!): Movie
AddMovieGenres(moviemovieId: ID!, genrename: String!): Movie
RemoveMovieGenres(moviemovieId: ID!, genrename: String!): Movie
AddMovieFilmedIn(moviemovieId: ID!, statename: String!): Movie
RemoveMovieFilmedIn(moviemovieId: ID!, statename: String!): Movie
AddMovieGenres(from: _MovieInput!, to: _GenreInput!): _AddMovieGenresPayload
RemoveMovieGenres(from: _MovieInput!, to: _GenreInput!): _RemoveMovieGenresPayload
AddMovieActors(from: _ActorInput!, to: _MovieInput!): _AddMovieActorsPayload
RemoveMovieActors(from: _ActorInput!, to: _MovieInput!): _RemoveMovieActorsPayload
AddMovieFilmedIn(from: _MovieInput!, to: _StateInput!): _AddMovieFilmedInPayload
RemoveMovieFilmedIn(from: _MovieInput!, to: _StateInput!): _RemoveMovieFilmedInPayload
AddMovieWatchedBy(from: _UserInput!, to: _MovieInput!, data: _WatchedInput!): _AddMovieWatchedByPayload
RemoveMovieWatchedBy(from: _UserInput!, to: _MovieInput!): _RemoveMovieWatchedByPayload
CreateGenre(name: String): Genre
DeleteGenre(name: String!): Genre
AddGenreMovies(from: _MovieInput!, to: _GenreInput!): _AddGenreMoviesPayload
RemoveGenreMovies(from: _MovieInput!, to: _GenreInput!): _RemoveGenreMoviesPayload
CreateActor(id: ID, name: String): Actor
UpdateActor(id: ID!, name: String): Actor
DeleteActor(id: ID!): Actor
AddActorMovies(actorid: ID!, moviemovieId: ID!): Actor
RemoveActorMovies(actorid: ID!, moviemovieId: ID!): Actor
AddActorMovies(from: _ActorInput!, to: _MovieInput!): _AddActorMoviesPayload
RemoveActorMovies(from: _ActorInput!, to: _MovieInput!): _RemoveActorMoviesPayload
CreateState(name: String): State
DeleteState(name: String!): State
CreateBook(genre: BookGenre): Book
DeleteBook(genre: BookGenre!): Book
CreateUser(id: ID, name: String): User
UpdateUser(id: ID!, name: String): User
DeleteUser(id: ID!): User
AddUserWatched(from: _UserInput!, to: _MovieInput!, data: _WatchedInput!): _AddUserWatchedPayload
RemoveUserWatched(from: _UserInput!, to: _MovieInput!): _RemoveUserWatchedPayload
CreateBook(genre: BookGenre): Book
DeleteBook(genre: BookGenre!): Book
}
interface Person {
Expand All @@ -129,8 +251,8 @@ type Query {
Genre(_id: Int, name: String, first: Int, offset: Int, orderBy: _GenreOrdering): [Genre]
Actor(id: ID, name: String, _id: Int, first: Int, offset: Int, orderBy: _ActorOrdering): [Actor]
State(name: String, _id: Int, first: Int, offset: Int, orderBy: _StateOrdering): [State]
Book(genre: BookGenre, _id: Int, first: Int, offset: Int, orderBy: _BookOrdering): [Book]
User(id: ID, name: String, _id: Int, first: Int, offset: Int, orderBy: _UserOrdering): [User]
Book(genre: BookGenre, _id: Int, first: Int, offset: Int, orderBy: _BookOrdering): [Book]
}
type State {
Expand All @@ -141,8 +263,15 @@ type State {
type User implements Person {
id: ID!
name: String
watched: [_UserWatched]
_id: Int
}
type Watched {
from(first: Int, offset: Int, orderBy: _UserOrdering): User
rating: Int
to(first: Int, offset: Int, orderBy: _MovieOrdering): Movie
}
`;

t.is(printSchema(schema), expectedSchema);
Expand Down
Loading

0 comments on commit c898ef5

Please sign in to comment.