Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a scalar JSON to graphql and use it for the authenticate params type #798

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/e2e/__tests__/databases/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class DatabaseTest implements DatabaseTestInterface {

public async start() {
(mongoose as any).Promise = global.Promise;
await mongoose.connect(connectionString);
await mongoose.connect(connectionString, { useNewUrlParser: true });
await mongoose.connection.dropDatabase();
}

Expand Down
3 changes: 0 additions & 3 deletions packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
"@accounts/typeorm": "^0.19.0",
"@accounts/types": "^0.19.0",
"@graphql-modules/core": "0.7.11",
"@types/lodash": "4.14.138",
"@types/mongoose": "5.5.17",
"@types/node-fetch": "2.5.0",
"apollo-boost": "0.4.4",
"apollo-server": "2.9.3",
"body-parser": "1.19.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/graphql-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@gql2ts/from-schema": "1.10.1",
"@gql2ts/types": "1.9.0",
"@graphql-modules/core": "0.7.11",
"@types/graphql-type-json": "0.3.2",
"@types/jest": "24.0.18",
"@types/request-ip": "0.0.34",
"concurrently": "4.1.2",
Expand All @@ -59,6 +60,7 @@
},
"dependencies": {
"graphql-toolkit": "^0.5.12",
"graphql-type-json": "0.3.0",
"request-ip": "2.1.3",
"tslib": "1.10.0"
}
Expand Down
31 changes: 14 additions & 17 deletions packages/graphql-api/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@ export interface TwoFactorSecretKeyInput {
otpauth_url?: Maybe<string>;
}

export interface AuthenticateParamsInput {
access_token?: Maybe<string>;

access_token_secret?: Maybe<string>;

provider?: Maybe<string>;

password?: Maybe<string>;

user?: Maybe<UserInput>;

code?: Maybe<string>;
}

export interface UserInput {
id?: Maybe<string>;

Expand All @@ -49,6 +35,12 @@ export interface UserInput {
username?: Maybe<string>;
}

export type Json = any;

// ====================================================
// Scalars
// ====================================================

// ====================================================
// Types
// ====================================================
Expand Down Expand Up @@ -184,10 +176,10 @@ export interface RefreshTokensMutationArgs {
export interface AuthenticateMutationArgs {
serviceName: string;

params: AuthenticateParamsInput;
params: Json;
}

import { GraphQLResolveInfo } from 'graphql';
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';

export type Resolver<Result, Parent = {}, TContext = {}, Args = {}> = (
parent: Parent,
Expand Down Expand Up @@ -498,7 +490,7 @@ export type MutationAuthenticateResolver<
export interface MutationAuthenticateArgs {
serviceName: string;

params: AuthenticateParamsInput;
params: Json;
}

export interface LoginResultResolvers<TContext = {}, TypeParent = LoginResult> {
Expand Down Expand Up @@ -592,6 +584,10 @@ export interface DeprecatedDirectiveArgs {
reason?: string;
}

export interface JSONScalarConfig extends GraphQLScalarTypeConfig<Json, any> {
name: 'JSON';
}

export type IResolvers<TContext = {}> = {
Query?: QueryResolvers<TContext>;
TwoFactorSecretKey?: TwoFactorSecretKeyResolvers<TContext>;
Expand All @@ -601,6 +597,7 @@ export type IResolvers<TContext = {}> = {
LoginResult?: LoginResultResolvers<TContext>;
Tokens?: TokensResolvers<TContext>;
ImpersonateReturn?: ImpersonateReturnResolvers<TContext>;
Json?: GraphQLScalarType;
} & { [typeName: string]: never };

export type IDirectiveResolvers<Result> = {
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql-api/src/modules/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { AuthenticatedDirective } from '../../utils/authenticated-directive';
import { context } from '../../utils';
import AccountsPassword from '@accounts/password';
import { mergeTypeDefs } from 'graphql-toolkit';
import GraphQLJSON from 'graphql-type-json';

export interface AccountsRequest {
req: IncomingMessage;
Expand Down Expand Up @@ -61,6 +62,8 @@ export const AccountsModule: GraphQLModule<
),
resolvers: ({ config }) =>
({
// Inject JSON custom scalar
JSON: GraphQLJSON,
[config.rootQueryName || 'Query']: Query,
[config.rootMutationName || 'Mutation']: Mutation,
User: UserResolvers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default (config: AccountsModuleConfig) => gql`

# Example: Login with password
# authenticate(serviceName: "password", params: {password: "<pw>", user: {email: "<email>"}})
authenticate(serviceName: String!, params: AuthenticateParamsInput!): LoginResult
authenticate(serviceName: String!, params: JSON!): LoginResult
}
`;
16 changes: 1 addition & 15 deletions packages/graphql-api/src/modules/accounts/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AccountsModuleConfig } from '..';

export default ({ userAsInterface }: AccountsModuleConfig) => gql`
directive @auth on FIELD_DEFINITION | OBJECT
scalar JSON

type Tokens {
refreshToken: String
Expand Down Expand Up @@ -36,19 +37,4 @@ export default ({ userAsInterface }: AccountsModuleConfig) => gql`
email: String
username: String
}

input AuthenticateParamsInput {
# Twitter, Instagram
access_token: String
# Twitter
access_token_secret: String
# OAuth
provider: String
# Password
password: String
# Password
user: UserInput
# Two factor
code: String
}
`;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gql from 'graphql-tag';

export const loginWithServiceMutation = gql`
mutation($serviceName: String!, $params: AuthenticateParamsInput!) {
mutation($serviceName: String!, $params: JSON!) {
authenticate(serviceName: $serviceName, params: $params) {
sessionId
tokens {
Expand Down
18 changes: 15 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1364,9 +1364,9 @@
integrity sha512-dBtBbrc+qTHy1WdfHYjBwRln4+LWqASWakLHsWHR2NWHIFkv4W3O070IGoGLEBrJBvct3r0L1BUPuvURi7kYUQ==

"@types/babel__core@^7.1.0":
version "7.1.2"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.2.tgz#608c74f55928033fce18b99b213c16be4b3d114f"
integrity sha512-cfCCrFmiGY/yq0NuKNxIQvZFy9kY/1immpSpTngOnyIbD4+eJOG5mxphhHDv3CHL9GltO4GcKr54kGBg3RNdbg==
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.3.tgz#e441ea7df63cd080dfcd02ab199e6d16a735fc30"
integrity sha512-8fBo0UR2CcwWxeX7WIIgJ7lXjasFxoYgRnFHUj+hRvKkpiBJbxhdAPTCY6/ZKM0uxANFVzt4yObSLuTiTnazDA==
dependencies:
"@babel/parser" "^7.1.0"
"@babel/types" "^7.0.0"
Expand Down Expand Up @@ -1508,6 +1508,13 @@
"@types/minimatch" "*"
"@types/node" "*"

"@types/[email protected]":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@types/graphql-type-json/-/graphql-type-json-0.3.2.tgz#1a7105e6546fc1630a5db4834bfbc0eb554986e4"
integrity sha512-c1cq4o8EhY0Z39ua8UXwG8uBs23xBYA/Uw0tXFl6SuTUpkVv/IJqf6pHQbfdC7nwFRhX2ifTOV/UIg0Q/IJsbg==
dependencies:
graphql "^14.5.3"

"@types/graphql-upload@^8.0.0":
version "8.0.3"
resolved "https://registry.yarnpkg.com/@types/graphql-upload/-/graphql-upload-8.0.3.tgz#b371edb5f305a2a1f7b7843a890a2a7adc55c3ec"
Expand Down Expand Up @@ -6615,6 +6622,11 @@ [email protected], [email protected], graphql-tools@^4.0.0, graphql-tools@^4
iterall "^1.1.3"
uuid "^3.1.0"

[email protected]:
version "0.3.0"
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.3.0.tgz#bb32e1b74bf52ebc690f9df12b4067bc061f818a"
integrity sha512-lnxg5HiB95yxy+/5cDKtP6pZo0zgntsOmqsjeCBXFGJ4YoMF3+1YaSEKWJntNTu+VsAm3zf6lPxFpp1kxzofLA==

graphql-upload@^8.0.2:
version "8.0.7"
resolved "https://registry.yarnpkg.com/graphql-upload/-/graphql-upload-8.0.7.tgz#8644264e241529552ea4b3797e7ee15809cf01a3"
Expand Down