You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been learning Nexus and I've been trying to implement a unionType that returns either a LoginSuccess, with its respective fields, or a LoginError -- the implementation could be applied to any other context, I suppose. However, I keep running into an error, and I can't wrap my head around it:
Error: Expected LoginSuccess to be a objectType, saw GraphQLScalarType
LoginSuccess is an objectType as seen below, so how come Nexus doesn't recognize it as such?
The code in question:
// src/graphql/types/auth/loginUser.ts
export const LoginUser = extendType({
type: "Mutation",
definition: (t) => {
t.nonNull.field("loginUser", {
type: LoginResult,
args: {
credentials: nonNull(LoginCredentials),
},
resolve: loginUser,
});
},
});
const LoginSuccess = objectType({
name: "LoginSuccess",
definition: (t) => {
t.string("sid");
t.id("id");
t.boolean("loggedIn");
},
});
const LoginError = objectType({
name: "LoginError",
definition: (t) => {
t.boolean("error");
t.string("message");
},
});
// Type
const LoginResult = unionType({
name: "LoginResult",
definition: (t) => {
t.members(LoginSuccess, LoginError);
},
resolveType: (data) => {
const __typename =
"username" in data
? "LoginSuccess"
: "error" in data
? "LoginError"
: null;
if (!__typename) {
throw new Error(
`Could not resolve the type of data passed to union type "LoginResult"`
);
}
return __typename;
},
});
// Args
const LoginCredentials = inputObjectType({
name: "LoginCredentials",
definition: (t) => {
t.nonNull.string("username");
t.nonNull.string("password");
},
});
I am also exporting the type, and importing it into my schema:
// src/graphql/types/index.ts
export * from "./auth/loginUser";
Hello guys,
I've been learning Nexus and I've been trying to implement a
unionType
that returns either aLoginSuccess
, with its respective fields, or aLoginError
-- the implementation could be applied to any other context, I suppose. However, I keep running into an error, and I can't wrap my head around it:LoginSuccess
is anobjectType
as seen below, so how come Nexus doesn't recognize it as such?The code in question:
I am also exporting the type, and importing it into my schema:
My
package.json
dependencies:I could be overlooking something really simple here, but any help with this would be very much appreciated!
Thank you.
The text was updated successfully, but these errors were encountered: