Axios和Apollo服务器-类型错误[ERR_INVALID_ARG_TYPE]:

yzuktlbb  于 2023-02-04  发布在  iOS
关注(0)|答案(1)|浏览(210)

我开始学习Graphql,我正在用Axios编写一个服务器。当我访问Apollo的沙箱时,当页面加载时,我得到了下面的错误。我不知道错误在哪里,发生了什么。
下面是我的索引.ts:

import { ApolloServer } from 'apollo-server'
    import { resolvers, typeDefs } from './graphql/schema'
    import 'dotenv/config'
    import axios from "axios"
    
    const server = new ApolloServer({
        typeDefs: typeDefs,
        resolvers: enter code hereresolvers,
    
        context: () => {
            return axios
        }
    })
    server.listen(process.env.SERVER_PORT).then((url) => {
        console.log(`Server listening on url ${url.url}`)
    })

这是我的解析器。ts

const user = async (_: any, {id}: any, {axios}: any, info: any) => {
    const user = await axios.get("http://localhost:3000/user/1/")
    return user.data
}
// 2ª variável: parâmetros da consulta
// 3ª variável: context
const users = async (_: any, __: any, {axios}: any, info: any) => {
    const users = await axios.get("http://localhost:3000/users")
    return users.data
}

export const userResolvers = {
    Query: {
        user,
        users
    }
}

下面是我的typedefs.ts

import { gql } from "apollo-server";
    
    export const UserTypeDefs = gql`
            extend type Query {
                user(id: ID!): User! # O símbolo '!' indica obrigatoriedade na consulta
                users: [User!]!
            }
            type User {
                id: ID!
                username: String!
                firstName: String!
                lastName: String!
                indexRef: Int!
                createdAt: String!
                #  Posts associados
                # posts: [Posts!]!
            }`

下面是错误:

$ nodemon --exec ts-node ./src/index.ts
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node ./src/index.ts`
Server listening on url http://localhost:4003/
TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:120:11)
    at Url.parse (node:url:169:3)
    at Object.urlParse [as parse] (node:url:156:13)
    at dispatchHttpRequest (D:\Cliente\Diversos\graphql\node_modules\axios\lib\adapters\http.js:133:22)
    at new Promise (<anonymous>)
    at httpAdapter (D:\Cliente\Diversos\graphql\node_modules\axios\lib\adapters\http.js:49:10)
    at dispatchRequest (D:\Cliente\Diversos\graphql\node_modules\axios\lib\core\dispatchRequest.js:58:10)
    at Axios.request (D:\Cliente\Diversos\graphql\node_modules\axios\lib\core\Axios.js:109:15)
    at Object.wrap [as context] (D:\Cliente\Diversos\graphql\node_modules\axios\lib\helpers\bind.js:9:15) {
  code: 'ERR_INVALID_ARG_TYPE'
}
[nodemon] app crashed - waiting for file changes before starting...
3zwjbxry

3zwjbxry1#

url需要被解构;所以只需要将index.ts的最后一部分修改为:

server.listen(process.env.SERVER_PORT).then(({ url }) => {
        console.log(`Server listening on url ${url}`)
    })

相关问题