Skip to content

Commit

Permalink
Merge pull request #227 from trilliput/feature/multiple_schemas_62
Browse files Browse the repository at this point in the history
Support for multiple schema files
  • Loading branch information
sid88in authored Mar 17, 2019
2 parents 09abc44 + ee25367 commit 5a3420a
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ custom:
request: # request mapping template name
response: # response mapping template name
- ${file({fileLocation}.yml)} # link to a file with arrays of mapping templates
schema: # defaults schema.graphql
schema: # schema file or array of files to merge, defaults to schema.graphql
dataSources:
- type: AMAZON_DYNAMODB
name: # data source name
Expand Down
194 changes: 194 additions & 0 deletions __snapshots__/get-config.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,199 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Schema as array 1`] = `
Object {
"apiId": undefined,
"apiKey": undefined,
"authenticationType": "AWS_IAM",
"dataSources": Array [],
"functionConfigurations": Array [],
"functionConfigurationsLocation": "mapping-templates",
"isSingleConfig": true,
"logConfig": undefined,
"mappingTemplates": Array [],
"mappingTemplatesLocation": "mapping-templates",
"name": "api",
"openIdConnectConfig": undefined,
"region": "us-east-1",
"schema": "schema {
query: Query
mutation: Mutation
subscription: Subscription
}
type Query {
# search functionality is available in elasticsearch integration
searchAllTweetsByKeyword(keyword: String!): TweetConnection
getUserInfo(handle: String!, consumer_key: String, consumer_secret: String): User!
meInfo(consumer_key: String, consumer_secret: String): User!
}
type Mutation {
# Create a tweet for a user
# consumer keys and tokens are not required for dynamo integration
createTweet(tweet: String!, consumer_key: String, consumer_secret: String, access_token_key: String, access_token_secret: String, created_at: String!): Tweet!
# Delete User Tweet
deleteTweet(tweet_id: String!, consumer_key: String, consumer_secret: String, access_token_key: String, access_token_secret: String): Tweet!
# Retweet existing Tweet
reTweet(tweet_id: String!, consumer_key: String, consumer_secret: String, access_token_key: String, access_token_secret: String): Tweet!
# Update existing Tweet
updateTweet(tweet_id: String!, tweet: String!): Tweet!
# Create user info is available in dynamo integration
updateUserInfo(location: String!, description: String!, name: String!, followers_count: Int!, friends_count: Int!, favourites_count: Int!, followers: [String!]!): User!
}
type Subscription {
addTweet: Tweet @aws_subscribe(mutations: [\\"createTweet\\"])
}
type Tweet {
tweet_id: String!
tweet: String!
retweeted: Boolean
retweet_count: Int
favorited: Boolean
created_at: String!
}
type TweetConnection {
items: [Tweet!]!
nextToken: String
}
type User {
name: String!
handle: String!
location: String!
description: String!
followers_count: Int!
friends_count: Int!
favourites_count: Int!
followers: [String!]!
topTweet: Tweet
tweets(limit: Int!, nextToken: String): TweetConnection
# search functionality is available in elasticsearch integration
searchTweetsByKeyword(keyword: String!): TweetConnection
}
",
"substitutions": Object {},
"userPoolConfig": undefined,
}
`;

exports[`Schema as string 1`] = `
Object {
"apiId": undefined,
"apiKey": undefined,
"authenticationType": "AWS_IAM",
"dataSources": Array [],
"functionConfigurations": Array [],
"functionConfigurationsLocation": "mapping-templates",
"isSingleConfig": true,
"logConfig": undefined,
"mappingTemplates": Array [],
"mappingTemplatesLocation": "mapping-templates",
"name": "api",
"openIdConnectConfig": undefined,
"region": "us-east-1",
"schema": "type Mutation {
# Create a tweet for a user
# consumer keys and tokens are not required for dynamo integration
createTweet(
tweet: String!,
consumer_key: String,
consumer_secret: String,
access_token_key: String,
access_token_secret: String,
created_at: String!
): Tweet!
# Delete User Tweet
deleteTweet(
tweet_id: String!,
consumer_key: String,
consumer_secret: String,
access_token_key: String,
access_token_secret: String
): Tweet!
# Retweet existing Tweet
reTweet(
tweet_id: String!,
consumer_key: String,
consumer_secret: String,
access_token_key: String,
access_token_secret: String
): Tweet!
# Update existing Tweet
updateTweet(tweet_id: String!, tweet: String!): Tweet!
# Create user info is available in dynamo integration
updateUserInfo(
location: String!,
description: String!,
name: String!,
followers_count: Int!,
friends_count: Int!,
favourites_count: Int!,
followers: [String!]!
): User!
}
type Query {
meInfo(consumer_key: String, consumer_secret: String): User!
getUserInfo(handle: String!, consumer_key: String, consumer_secret: String): User!
# search functionality is available in elasticsearch integration
searchAllTweetsByKeyword(keyword: String!): TweetConnection
}
type Subscription {
addTweet: Tweet
@aws_subscribe(mutations: [\\"createTweet\\"])
}
type Tweet {
tweet_id: String!
tweet: String!
retweeted: Boolean
retweet_count: Int
favorited: Boolean
created_at: String!
}
type TweetConnection {
items: [Tweet!]!
nextToken: String
}
type User {
name: String!
handle: String!
location: String!
description: String!
followers_count: Int!
friends_count: Int!
favourites_count: Int!
followers: [String!]!
topTweet: Tweet
tweets(limit: Int!, nextToken: String): TweetConnection
# search functionality is available in elasticsearch integration
searchTweetsByKeyword(keyword: String!): TweetConnection
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
",
"substitutions": Object {},
"userPoolConfig": undefined,
}
`;

exports[`authenticationType is missing 1`] = `"appSync property \`authenticationType\` is missing or invalid."`;

exports[`authenticationType is missing 2`] = `"appSync property \`authenticationType\` is missing or invalid."`;
Expand Down
63 changes: 63 additions & 0 deletions example/_type_tweet.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
type Mutation {
# Create a tweet for a user
# consumer keys and tokens are not required for dynamo integration
createTweet(
tweet: String!,
consumer_key: String,
consumer_secret: String,
access_token_key: String,
access_token_secret: String,
created_at: String!
): Tweet!

# Delete User Tweet
deleteTweet(
tweet_id: String!,
consumer_key: String,
consumer_secret: String,
access_token_key: String,
access_token_secret: String
): Tweet!

# Retweet existing Tweet
reTweet(
tweet_id: String!,
consumer_key: String,
consumer_secret: String,
access_token_key: String,
access_token_secret: String
): Tweet!

# Update existing Tweet
updateTweet(tweet_id: String!, tweet: String!): Tweet!

}

type Query {
# search functionality is available in elasticsearch integration
searchAllTweetsByKeyword(keyword: String!): TweetConnection
}

type Subscription {
addTweet: Tweet @aws_subscribe(mutations: ["createTweet"])
}

type Tweet {
tweet_id: String!
tweet: String!
retweeted: Boolean
retweet_count: Int
favorited: Boolean
created_at: String!
}

type TweetConnection {
items: [Tweet!]!
nextToken: String
}

schema {
query: Query
mutation: Mutation
subscription: Subscription
}
34 changes: 34 additions & 0 deletions example/_type_user.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type Mutation {
# Create user info is available in dynamo integration
updateUserInfo(
location: String!,
description: String!,
name: String!,
followers_count: Int!,
friends_count: Int!,
favourites_count: Int!,
followers: [String!]!
): User!
}

type Query {
getUserInfo(handle: String!, consumer_key: String, consumer_secret: String): User!
meInfo(consumer_key: String, consumer_secret: String): User!
}

type User {
name: String!
handle: String!
location: String!
description: String!
followers_count: Int!
friends_count: Int!
favourites_count: Int!
followers: [String!]!
topTweet: Tweet
tweets(limit: Int!, nextToken: String): TweetConnection

# search functionality is available in elasticsearch integration
searchTweetsByKeyword(keyword: String!): TweetConnection
}

16 changes: 8 additions & 8 deletions get-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { mergeTypes } = require('merge-graphql-schemas');
const {
mapObjIndexed, pipe, values, merge,
} = require('ramda');
Expand Down Expand Up @@ -39,15 +40,14 @@ const getConfig = (config, provider, servicePath) => {
const functionConfigurations = config.functionConfigurations || [];
const mappingTemplates = config.mappingTemplates || [];

const schemaPath = path.join(
servicePath,
config.schema || 'schema.graphql',
);
const readSchemaFile =
(schemaRelPath) => fs.readFileSync(path.join(servicePath, schemaRelPath), {encoding: 'utf8'});

const schemaContent =
Array.isArray(config.schema) ?
mergeTypes(config.schema.map(readSchemaFile)) :
readSchemaFile(config.schema || 'schema.graphql');

const schemaContent = fs.readFileSync(schemaPath, {
encoding: 'utf8',
});

let dataSources = [];
if (Array.isArray(config.dataSources)) {
dataSources = config.dataSources.reduce(
Expand Down
26 changes: 25 additions & 1 deletion get-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,28 @@ test('datasources as array form different files (array of arrays or objects)', (
{ region: 'us-east-1' },
servicePath
)[0]).toMatchSnapshot();
});
});

test('Schema as string', () => {

expect(getConfig(
{
authenticationType: 'AWS_IAM',
schema: 'schema.graphql'
},
{ region: 'us-east-1' },
servicePath
)[0]).toMatchSnapshot();
});

test('Schema as array', () => {

expect(getConfig(
{
authenticationType: 'AWS_IAM',
schema: ['_type_tweet.graphql', '_type_user.graphql']
},
{ region: 'us-east-1' },
servicePath
)[0]).toMatchSnapshot();
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"graphql": "^0.13.2",
"graphql-playground-middleware-koa": "^1.6.4",
"koa": "^2.5.2",
"merge-graphql-schemas": "^1.5.8",
"ramda": "^0.25.0"
},
"peerDependencies": {},
Expand Down
Loading

0 comments on commit 5a3420a

Please sign in to comment.